awk variables

Harald Fuchs fuchs at it.uka.de
Sat Dec 1 23:18:47 AEST 1990


oscarh at hpdmd48.boi.hp.com (Oscar Herrera) writes:
>>I am trying to use both shell variables and awk variables in the 
>>same awk call, but I cannot get both to work correctly.  I have tried
>>many variations of quoting and have had no success.  The statement
>>I want to use is :
>>
>>	awk "NR == $line_num {print $1}" filename
>>
>>where $line_num is a script variable (an integer) and $1 is the 
>>awk variable for the first field.

>	try

> 	awk 'NR == '$line_num '{print $1}' filename

>	Having $line_num not enclosed in single quotes should 
>	allow the shell to get a hold of it and stick the variable value
>	in its place.

This won't work.
  awk 'NR == '$line_num '{print $1}' filename
                       ^ Here's the bug
Awk expects its script to be the first argument, and when there's a
blank after $line_num (or within an expanded shell variable like that)
awk will see two arguments. Don't put blanks around $line_num and, if
your shell variable might contain blanks, enclose it by double quotes:
  awk 'NR == '"$line_num"'{print $1}' filename
--

Harald Fuchs <fuchs at it.uka.de> <fuchs%it.uka.de at relay.cs.net> ...
<fuchs at telematik.informatik.uni-karlsruhe.dbp.de>   *gulp*



More information about the Comp.unix.shell mailing list