Awk with passed parameters

Art Neilson art at pilikia.pegasus.com
Tue Mar 12 04:34:26 AEST 1991


In article <1991Mar10.235414.28125 at athena.mit.edu> jik at athena.mit.edu (Jonathan I. Kamens) writes:
>In article <1991Mar08.141340.26881 at convex.com>, tchrist at convex.COM (Tom Christiansen) writes:
>|> The first thing is that you need to put your variable assignments
>|> in front of your program.  You probably should protect your string
>|> literals with double quotes so it doesn't think they are variables.
>
>  This is not true.  Awk reads variable assignments in "file arguments" and
>does the assignments *before* the main body of the awk program is executed. 
>The only problem with the program as originally posted was that he needed to
>add "-" to the end of the awk command to tell it to read from stdin after
>reading the other "file arguments" (which were actually variable assignments).
>
>  If he adds that dash, then things should work on virtually any version of
>awk, since variable assignments in file arguments were supported in very early
>versions of awk and have continued to be supported in most versions.  Tom, do
>you know of a version of awk which won't work properly if an extra file
>argument of "-" is added to the end of the awk command?

Hey! use of that dash is a nifty trick.  I have always done shell quoting
tricks to pass shell parameters to old awk, the dash technique is a much
better method.  The following works fine on my system, although I need a
+ 0 on the day passed from the date command output in order to make it
compare correctly with the day field in the ls command output (for days
of the month between 1 and 9).  This works for me:

set `date`
ls -l | awk '$6 == mm && $7 == dd + 0 { print }' mm=$2 dd=$3 -
-- 
Arthur W. Neilson III		| INET: art at pilikia.pegasus.com
Bank of Hawaii Tech Support	| UUCP: uunet!ucsd!nosc!pilikia!art



More information about the Comp.unix.questions mailing list