Awk question

Reply

Join Date: Apr 2008
Posts: 2
Reputation: mooglor is an unknown quantity at this point 
Solved Threads: 0
mooglor mooglor is offline Offline
Newbie Poster

Awk question

 
0
  #1
Apr 3rd, 2008
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2. ARCHIVE_PATH=/archive
  3. CURRENT_DATE='date +%F%H%M%S'
  4. echo running at `date` >>~/Logs.txt
  5.  
  6. for FolderToSearch in $(ls -l /home/stuff/tp |grep ^d|awk '{print $9}');
  7. do find /home/stuff/tp/$FolderToSearch/in -name '*.*'|awk '{print "zip -m " $ARCHIVE_PATH "_" $FolderToSearch "zip2arc.zip" $CURRENT_DATE;}'|bash|tee -a ~/Logs.txt;
  8. done

Hi there,
I'd appreciate it if anyone can tell me how to use variables within the print statement of an awk statement. The above code is meant to
1. List a directory.
2. For each subdirectory, list the files in its in/ folder.
3. Zip these files into a /archive/zip file (also removing them zip -m)
Ideally, the zip file would have the name of the folder it came from as a prefix of its filename and the date as postfix.

The problem is that $CURRENT_DATE and $ARCHIVE_PATH are not expanded to their values. I've tried using single quotes etc.Note that this is on AIX, where find and awk, although very similar, are a little bit primitive compared to their GNU counterparts.
If anyone can recommend a good hair transplant clinic too I'd be eternally in your debt.

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 2
Reputation: mooglor is an unknown quantity at this point 
Solved Threads: 0
mooglor mooglor is offline Offline
Newbie Poster

Re: Awk question

 
0
  #2
Apr 3rd, 2008
I just noticed that I pasted a small problem, missing the $1.
Just to clarify a little:

My problem is specifically with this line:

Shell Scripting Syntax (Toggle Plain Text)
  1. awk '{print "zip -m " $ARCHIVE_PATH $FolderToSearch "zip2arc.zip" $CURRENT_DATE " " $1;}'

I expect it to ouput this:
zip -m archive/Folder1zip2arc.zip2008-04-03113933 Filename1

But it outputs:
zip -m zip2arc.zip Filename1

...leaving out all of the variables.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Awk question

 
0
  #3
Apr 3rd, 2008
Hey There,

The issue may be resolved by just using double quotes around your statement to expand variables from the shell within awk, like:

Shell Scripting Syntax (Toggle Plain Text)
  1. awk "{print "zip -m " $ARCHIVE_PATH $FolderToSearch "zip2arc.zip" $CURRENT_DATE " " $1;}"

If you want to specifically set a variable within the awk statement, you can set it like

Shell Scripting Syntax (Toggle Plain Text)
  1. var=value

and then expand it using

Shell Scripting Syntax (Toggle Plain Text)
  1. print $var

It can be a bit tricky if you want to combine the two, since $1 in awk is distinctly different from $1 in the shell, depending.

Hopefully some of that is helpful

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 2
Reputation: chrisgood60 is an unknown quantity at this point 
Solved Threads: 0
chrisgood60 chrisgood60 is offline Offline
Newbie Poster

Re: Awk question

 
0
  #4
Apr 4th, 2008
The problem is that those variables are within single quotes so are not expanded.
You need to end the single quote before the variable and start it again after the variable when building your awk script
eg echo 'This is singlequoted - TERM='$TERM' this is also single quoted'
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 39
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Awk question

 
0
  #5
Apr 5th, 2008
use the -v option of awk to pass in shell variables so that you don't have to get too confused between shell and awk variables.
eg
Shell Scripting Syntax (Toggle Plain Text)
  1. awk -v archive="$ARCHIVE" ' {
  2. print archive
  3. # etc code
  4. }
  5.  

also you do not need to use ls , grep and awk just to get directory names. you can use find.
Shell Scripting Syntax (Toggle Plain Text)
  1. for foldersearch in `find /path -type d`
  2. do
  3. ...
  4. done
Last edited by ghostdog74; Apr 5th, 2008 at 6:24 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC