awk arguments

William Bill Mayne mayne at VSSERV.SCRI.FSU.EDU
Mon Jul 23 23:56:42 AEST 1990


I have had a problem with the syntax of the awk command for some
time. I quote from the man pages for awk from SunOS:
> 
> SYNOPSIS
>      awk [ -f program-file ] [ -Fc  ]  [  program  ]  [  variable
>      =value ... ] [ filename...]
> 
Since unix file names may contain an = this is clearly ambiguous.
Does

      awk '{print $0; print $Y}' Y=X

print lines from standard input separated by a line containing "X"
or does it print lines from the file Y=X separated by blank lines,
since without the assignment the value Y in awk should be null?
Either interpretation would be justified by the syntax. 
The command could be parsed where Y=X is either [variable=value]
or [filename...]. In fact it does neither. In this case, whether a
file named Y=X exists or not, standard input is not read and
nothing is printed.

The practical problem this raises is how to communicate an
argument value or a value calculated by a script file into
an awk program embedded in a script file. Referencing $n in
the quoted awk program won't work, since awk will think that
refers to field number n. Similarly any other reference to a
shell variable will be interpreted as a reference to an awk
variable.

I have never been able to get the command line assignment of 
awk variables to work. I have sometimes resorted to ugly kludges 
like using sed to modify an awk program to hard code argument 
values. Surely I have missed something obvious. There must be a 
better way. I would be most grateful if someone would suggest one.

While I am on the subject of awk: I learned about the language
from the book "The AWK Programming Language" by Kernighan et. al.
before I started working on unix. I have been disappointed to
find that the version actually available on every flavor of unix
I have seen is much weaker than the full version described by the
book. Are there better versions out there?



More information about the Comp.unix.wizards mailing list