Results 1 to 6 of 6
I have a directory for every area code 200-999, and inside each directory is a directory for each prefix that we have added, and one file name 'prefixes' that contains ...
- 12-21-2011 #1Just Joined!
- Join Date
- Dec 2011
- Posts
- 7
Dynamic File Editing
I have a directory for every area code 200-999, and inside each directory is a directory for each prefix that we have added, and one file name 'prefixes' that contains one prefix per line like this '200,0,0,0,0,0,0,0,0'.
Right now every 'prefixes' file contains prefixes 200-999, but if we try to enter a phone number without the prefix directory it crashes the system.
So, I need a script to check each area code directory and remove all the prefix lines from the 'prefixes' file that don't have a corresponding prefix directory.
I hope this makes sense, I am completely lost and desperate for help.
- 12-23-2011 #2Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
I don't understand your directory structure. The way you describe it, it sounds like you'd have the following directories (and one file):
Clearly that is not possible (you can't have more than one directory/file of the same name in a directory). Can you maybe post an example "prefixes" file and explain your directory structure more clearly? What you want to do is pretty straight forward, fwiw, so I'm sure we can help you.Code:/200/ /200/prefixes /200/0/ /200/0/ /200/0/ /200/0/ /200/0/ /200/0/ /200/0/ /200/0/
- 12-23-2011 #3
I figure that the file structure is something like
But I am entirely uncertain of the relationship between the directories, files and dataCode:200/prefix1/ 200/prefix2/ 200/prefix3/ 200/prefix (File contains rows like 200,0,0,0,0,0,0,0,0) 201/prefix1/ 201/prefix2/ 201/prefix3/ 201/prefix (File contains rows like 201,0,0,0,0,0,0,0,0) ... 999/prefix1/ 999/prefix2/ 999/prefix3/ 999/prefix (File contains rows like 999,0,0,0,0,0,0,0,0)
If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 12-23-2011 #4Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
@elija, if you're right, then I don't understand the relationship b/t the contents of the prefix files and the subdirs. but then, i don't understand much that is not whacked over my head repeatedly.
- 12-28-2011 #5Just Joined!
- Join Date
- Dec 2011
- Posts
- 7
Here is an example of the directory structure...
200/prefixes
200/200/*files*
200/201/*files*
200/202/*files*
200/203/*files*
200/204/*files*
200/205/*files*
etc...
201/prefixes
201/200/*files*
201/201/*files*
201/202/*files*
201/203/*files*
201/204/*files*
201/205/*files*
etc...
The 'prefixes' file located in each area code directory is formatted like this, for each prefix in the area code directory...
200,0,0,0,0,0,0,0,0
201,0,0,0,0,0,0,0,0
202,0,0,0,0,0,0,0,0
203,0,0,0,0,0,0,0,0
204,0,0,0,0,0,0,0,0
205,0,0,0,0,0,0,0,0
etc...
- 12-28-2011 #6Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
Try this out. I tested it and it worked for my mock setup. I only had 3 prefix dirs and like 10 sub-prefixdirs, so I can't say how long it'll take to run, but hopefully it will get you started.
It will run off the current working dir, so copy it to a script and stick it in the root of your initial dirs. Say, if you name it "fix-prefixes.sh", then you might have a dir structure like:
/tmp/
/tmp/fix-prefixes.sh
/tmp/200/
/tmp/200/prefixes
/tmp/200/200
/tmp/200/201
/tmp/200/202
/tmp/201/
/tmp/201/prefixes
/tmp/201/200
/tmp/201/201
/tmp/201/202
etc.
and you'd run it with:\I tried to annotate the code with comments, so I don't have to explain here what it does. Let me know how it doesn't work.Code:chmod +x /tmp/fix-prefixes.sh cd /tmp ./fix-prefixes.sh
Note: as it is, the code will modify your existing "prefixes" files that it finds (see around line 55 - the "cp" command). So please test the code on dirs you don't care about first, or edit the code to not do the actual cp command.
Code:#!/bin/bash # find dirs (non-recursively) in the current working directory for dir in $(find . -maxdepth 1 -type d -not -path '.'); do # recycle some variables unset prefixDirs unset prefixesInFile printf "\nDir: $dir\n" # the prefixes file prefixFile="${dir}/prefixes" if ! [ -f $prefixFile ]; then echo "No prefixes file \`$prefixFile' found - skipping this dir" continue fi # a temporary prefix file prefixTmp="${dir}/.prefixes.tmp" # if it exists, remove it [ -f $prefixTmp ] && /bin/rm -f $prefixTmp # get list of directories in this dir (the prefix dirs) prefixDirs=$(find $dir -maxdepth 1 -type d -not -path "$dir" -printf "%f ") # get the prefixes (1st column) in the "prefixes" file prefixesInFile=$(awk -F, '{print $1}' $prefixFile) # loop thru this list for pre in $prefixesInFile; do unset preFound # loop thru list of dirs generated earlier for realPre in $prefixDirs; do # see if prefix in 1st column is one of the actual sub-directories found if [ "$realPre" == "$pre" ]; then preFound=1 # jump out of inner loop break fi done # if this prefix was found, then write the corresponding line from the prefixes file to the temp prefix file if test $preFound; then grep ^$pre, $prefixFile >> $prefixTmp fi done # see if the temp prefix file was created if [ -e $prefixTmp ]; then # see if the temp prefix file differs from the original one cmp -s $prefixTmp $prefixFile if [ $? -eq 0 ]; then echo "No changes detected in prefixes" else echo "Copying temp prefix file to new one..." cp -f $prefixTmp $prefixFile || exit 1 fi else echo "No prefixes in $prefixFile appear to exist" fi done


Reply With Quote