Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 7th, 2008, 12:03 PM
Newbie
 
Join Date: Aug 2007
Posts: 15
Default Shell Script awk problem , please advice

Hi, i am currently writing a script to delete the directory named with number.

Example:
in /usr/
i have
/usr/101
/usr/102 ...... /usr/200

This is the command i type and it works, i got the directory listed where the number is <150

Expand|Select|Wrap|Line Numbers
  1. ls -l /usr/ | grep ^d | awk '{ if ( $9 < 150) print $9 }'
So i applied it to the script, and it is not working especially at awk if else comparison, screen printed ALL number (101 -200). I am suspecting the error is in awk command there. Please help.

Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash -xv
  2. COPYKEEP="10"
  3. LATEST="200"
  4. KEEPFROM=`expr $LATEST - $COPYKEEP`
  5.  
  6. if [ $KEEPFROM -gt 0 ]
  7. then
  8.  
  9. # Find which to delete.
  10. ls -l /usr/ | grep ^d |  awk '{ if ( $9 < $KEEPFROM ) print $9 }'
  11. fi
Reply
  #2  
Old August 7th, 2008, 02:18 PM
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 297
Default

Expand|Select|Wrap|Line Numbers
  1. #!/bin/bash -xv
  2. COPYKEEP="10"
  3. LATEST="200"
  4. KEEPFROM=`expr $LATEST - $COPYKEEP`
  5. echo $KEEPFROM
  6. if [ $KEEPFROM -gt 0 ]
  7. then
  8. # Find which to delete.
  9. ls -l /usr/ | grep ^d |  awk -v p=$KEEPFROM '{ if ( $9 < p ) print $9 }'
  10. fi
  11.  
check the above script...
you can not use shell variables in awk directly..
it needs to be assigned to awk variable and then use it...
in Example we'r assigning it to 'p'
Reply
  #3  
Old August 11th, 2008, 03:19 AM
Newbie
 
Join Date: Aug 2007
Posts: 15
Default

It works!!

Thank you so much!
Reply
  #4  
Old August 11th, 2008, 10:15 AM
Newbie
 
Join Date: Aug 2007
Posts: 15
Default

Hi again,

i have tried to delete the file in awk using method found via google.

Expand|Select|Wrap|Line Numbers
  1. ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }'
This is working , however when i apply this to the script

Expand|Select|Wrap|Line Numbers
  1. echo " ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }' "
  2.  
The quoting is very confusing, can anyone help?
Reply
  #5  
Old August 11th, 2008, 11:02 AM
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 297
Default

Quote:
Originally Posted by DannyMc
Hi again,

i have tried to delete the file in awk using method found via google.

Expand|Select|Wrap|Line Numbers
  1. ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }'
This is working , however when i apply this to the script

Expand|Select|Wrap|Line Numbers
  1. echo " ls -l /usr1/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf " "\""$9"\"") }' "
  2.  
The quoting is very confusing, can anyone help?


I didn't need any complex quoting...
Next statement worked for me in shell script as well as on command line...

ls -l /usr/ | grep ^d | grep -v current | awk -v p=103 '{ if ( $9 < p ) system("rm -rf /usr/" $9) }'
Reply
  #6  
Old August 12th, 2008, 11:22 AM
Newbie
 
Join Date: Aug 2007
Posts: 15
Default

Again, Thank you. It works :)
Reply
  #7  
Old August 16th, 2008, 06:28 AM
Expert
 
Join Date: Apr 2006
Posts: 503
Default

there's actually another less cumbersome way to do it.
Expand|Select|Wrap|Line Numbers
  1. rm -rf /usr/[0-1][0-9][0-9]
  2. rm -rf /usr/200
  3.  
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles