awk arguments

Brian Dickson briand at trigraph.uucp
Wed Jul 25 09:51:05 AEST 1990


In article <290 at sun13.scri.fsu.edu> mayne at VSSERV.SCRI.FSU.EDU (William (Bill) Mayne) writes:
>Since unix file names may contain an = this is clearly ambiguous.
>
>[...]
>
>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.
>
>... Surely I have missed something obvious. There must be a 
>better way. I would be most grateful if someone would suggest one.

The problem is, ''-quotes hide special characters, which is why they
are often used. ""-quotes don't, thus letting shell substitutions occur.
But, you say, I want to use the $ in my awk script; how do I hide it from
the shell? The \ character inside "" will hide \,',", and $.
(Paraphrased from sh(1) on BSD4.3)

So, for example, the following script will take $1 as an assignment, $2
as a file, and run an awk script making use of the various combinations of
references to shell and awk variables.

	eval $1
	awk "{print \$0 \" $X $1 $2\"}" <$2

Call the file X=Y, and make it executable; see what "X=Y X=Y X=Y" does.

>
>... Are there better versions out there?
At our site we use nawk, which I believe is public domain. Consult an archive
site near you for more info.

--
Brian Dickson,
Trigraph Inc.
Disclaimer: Disclaimer is a trademark of AT&T.



More information about the Comp.unix.wizards mailing list