using /bin/sh vars in awk

Martin Weitzel martin at mwtech.UUCP
Wed May 1 23:23:24 AEST 1991


In article <1991Apr29.122714.17138 at athena.mit.edu> jik at athena.mit.edu (Jonathan I. Kamens) writes:
>In article <MAKI.91Apr29205859 at void.jit.dec.com>, maki at void.jit.dec.com (Watanabe Maki) writes:
>|> 	If your awk is nawk(New Awk), it's easy.
>|> 	
>|> 	... | nawk '{if ($1 == fred) command}' fred="barney"
>
>  Actually, this syntax will work with nearly any version of awk, not just
>nawk.  It was available, albeit undocumented, in even the earliest distributed
>versions.

Minor nitpick: But be sure to add a "-" if you want to have stdin processed.

 	... | nawk '{if ($1 == fred) command}' fred="barney" -
	# Important little thing here, watch closely! -------^

Reason: awk in its older versions is a bit stupid about the command line
arguments that are assignments to awk-variables and counts them as if they
were file arguments, failing process stdin as default if there are any
such assignments but no file arguments. The trick with explicitly naming
stdin solves this problem.
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.unix.shell mailing list