aaron.harnly.net

Triple-Quote to use a shell variable in awk

Just a little note: Found myself wanting to print a shell variable as part of an ‘awk’ one-liner, but found it rather surprisingly difficult. Here’s the magic tickle required:

x="foo"
awk '{ print "'"$x"'" }'

That’s a ” followed by ‘ and then another “.

For the curious, the actual script was a one-liner for dumping out Mac OS X application version information:

for x in /Applications/*.app 
do mdls $x | awk -F \" '/kMDItemVersion/ { print "'"$(basename $x)"'", $2 }'
done

cheers.

Digg this     Create a del.icio.us Bookmark     Add to Newsvine

One Response to “Triple-Quote to use a shell variable in awk”

  1. patrick Says:

    cool ! works great also with “printf” command ! thanks … for curious ;-) I use it with these lines: acc=$(ls -l –full-time –time=atime $fic | awk ‘{printf “%s,%s”,$6,substr($7,1,8)}’) echo $mod | awk ‘{printf “%s,%s,%s,%s,%s,%sn”,$9,$5,$6,substr($7,1,8),”‘”$acc”‘”,$3}’ This “trick” allows to get “acc” variable in 2nd line in awk command ! Thanks again Patrick

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word