![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
awk question
I'm writing a script that gets and removes all directories except for certain predefined ones. I'm currently using awk, but I admit that I'm not terribly comfortable with it. My current code works for directories that don't contain any spaces, but for those that do, it just passes the final word to rm, which obviously means that rm won't find it.
Code:
dirsToDelete=`ls -l|awk 'NF>2 &&!/Desktop/&&!/Documents/&&!/Library/&&!/Movies/&&!/Music/&&!/Pictures/&&!/Public/&&!/Sites/{print $NF}'`
for dir in $dirsToDelete; do
rm -r $dir
done
Any ideas? |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Change the IFS temporary, something like:
Code:
OIFS=$IFS
IFS=""
dirsToDelete=`ls -l|awk 'NF>2 &&!/Desktop/&&!/Documents/&&!/Library/&&!/Movies/&&!/Music/&&!/Pictures/&&!/Public/&&!/Sites/{print $NF}'`
for dir in $dirsToDelete; do
rm -r $dir
done
IFS=$OIFS
Last edited by Franklin52; 05-08-2008 at 10:27 AM. |
|
#3
|
|||
|
|||
|
Sorry, that doesn't do it. Still only returns the last word of multi-word folders.
|
|
#4
|
|||
|
|||
|
It can all be done within a single awk script so the for loop is of no use and replace ls -l (ell) with ls -1 (one)
Code:
ls -1 | awk 'NF>2 &&!/Desktop/&&!/Documents/&&!/Library/&&!/Movies/&&!/Music/&&!/Pictures/&&!/Public/&&!/Sites/{system("rm -r \""$0"\"")}'
|
|
#5
|
|||
|
|||
|
That did it. Thanks so much!
|
|
#6
|
||||
|
||||
|
...
[ksh] Code:
rm -r -- !(Desktop|Documents|Library|Movies|Music|Pictures|Public|Sites)/ Code:
shopt -s extglob rm -r -- !(Desktop|Documents|Library|Movies|Music|Pictures|Public|Sites)/ Code:
setopt extendedglob rm -r -- ^(Desktop|Documents|Library|Movies|Music|Pictures|Public|Sites)/ |
|
#7
|
|||
|
|||
|
radoulov:
I used your bash version, which works fine until it encounters one of the folders defined within the parentheses, at which point execution terminates and "kj#: command not found" is spit out. What's this mean/how do I get it to go away? |
|||
| Google The UNIX and Linux Forums |