The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 05-07-2008
Registered User
 

Join Date: Sep 2005
Posts: 6
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?
Reply With Quote
Forum Sponsor
  #2  
Old 05-07-2008
Moderator
 

Join Date: Feb 2007
Posts: 2,196
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
Regards

Last edited by Franklin52; 05-08-2008 at 10:27 AM.
Reply With Quote
  #3  
Old 05-08-2008
Registered User
 

Join Date: Sep 2005
Posts: 6
Sorry, that doesn't do it. Still only returns the last word of multi-word folders.
Reply With Quote
  #4  
Old 05-08-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
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"\"")}'
Reply With Quote
  #5  
Old 05-08-2008
Registered User
 

Join Date: Sep 2005
Posts: 6
That did it. Thanks so much!
Reply With Quote
  #6  
Old 05-08-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,855
...

[ksh]

Code:
rm -r -- !(Desktop|Documents|Library|Movies|Music|Pictures|Public|Sites)/
[bash]

Code:
shopt -s extglob
rm -r -- !(Desktop|Documents|Library|Movies|Music|Pictures|Public|Sites)/
[zsh]

Code:
setopt extendedglob
rm -r -- ^(Desktop|Documents|Library|Movies|Music|Pictures|Public|Sites)/
Reply With Quote
  #7  
Old 05-08-2008
Registered User
 

Join Date: Sep 2005
Posts: 6
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?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0