
passing vars to awk script
Quote:
> I'm sure this is something I should be able to figure out but...
> How do you pass variables into an awk script calling the awk from a korn shell
> script? I've tried:
> awk -f awkscript var1 var2 filename
> awk -f awkscript -v one=var1 -v two=var2 filename
> .. and then receiving them in the awk script as $1 and $2 in either the BEGIN
> section or as the first line in the action section.
> Neither works. I need to do it with awk -- not nawk.
1) old awk doesn't support -v, to my knowledge. That means that you
can't have access to the variables in the BEGIN section.
2) The correct syntax in that case is:
awk -f awkscript var1=one var2=two filename
And awkscript contains:
------------------------------------------------------------------------
BEGIN { print "var1 is \"", var1, "\"" }
/foo/ { print var1 }
/bar/ { print var2 }
------------------------------------------------------------------------
which for an input file consisting of the lines "foo" and "bar" produces:
------------------------------------------------------------------------
var1 is ""
one
two
------------------------------------------------------------------------
HTH.
--
Dom Mitchell -- Palmer & Harvey McLane -- Unix Systems Administrator
``Damn the philosophy, just provide the functionality and let anyone
with a competing philosophy come up with some better alternative if
they don't like it.'' -- JKH