echo running at `date` >>~/Logs.txt

for FolderToSearch in $(ls -l /home/stuff/tp |grep ^d|awk '{print $9}'); 
do find /home/stuff/tp/$FolderToSearch/in -name '*.*'|awk '{print "zip -m " $ARCHIVE_PATH "_" $FolderToSearch "zip2arc.zip" $CURRENT_DATE;}'|bash|tee -a ~/Logs.txt;

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.

8 Years
Discussion Span
Last Post by ghostdog74

I just noticed that I pasted a small problem, missing the $1.
Just to clarify a little:

My problem is specifically with this line:

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.


Hey There,

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

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


and then expand it using

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


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'


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.

awk -v archive="$ARCHIVE" ' { 
  print archive
  # etc code

also you do not need to use ls , grep and awk just to get directory names. you can use find.

for foldersearch in `find /path -type d`
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.