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.




May 25th, 2007 at 3:55 pm
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