Xref: utzoo comp.unix.questions:19210 gnu.misc.discuss:743 Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!swrinde!emory!audfax!arnold From: arnold@audiofax.com (Arnold Robbins) Newsgroups: comp.unix.questions,gnu.misc.discuss Subject: Re: Passing variables to gawk Message-ID: <196@audfax.audiofax.com> Date: 19 Jan 90 19:00:55 GMT References: Followup-To: comp.unix.questions Distribution: comp, gnu Organization: AudioFAX Inc., Atlanta Lines: 31 In article mikemc@mustang.ncr-fc.FtCollins.NCR.com (Mike McManus) writes: > >I want to pass variables to a gawk program via the command line. According to >the man page, all you need do is call gawk with = instead of file >names: > >gawk -f foo.awk a=1 b=2 c=3 infile > outfile > >and the variables a,b,c will be free to use in foo.awk (the gawk script), and >will be assigned the values 1,2,3. I can't seem to get this to work! When I >print the value of the variables inside a BEGIN{} block in foo.awk, they are all >set to 0. Anybody have any experience with this that they'd like to share? First, make sure you have gawk 2.11.1. Next, read the GAWK manual, and in particular, see the discussion of the -v option. Both gawk and the latest nawk now behave systematically; variable assignments are done *at the time they would have been opened as a file name*. This is always after the BEGIN block. Because earlier nawk implementations didn't behave quite the way they should have, the -v option was added to make assignments happen before the BEGIN block is run. So, what you want is gawk -f foo.awk -v a=1 -v b=2 -v c=3 -- infile > outfile which is not pretty, but works. The GAWK manual has more detail. -- Arnold Robbins -- Senior Research Scientist - AudioFAX | Laundry increases 2000 Powers Ferry Road, #220 / Marietta, GA. 30067 | exponentially in the INTERNET: arnold@audiofax.com Phone: +1 404 933 7600 | number of children. UUCP: emory!audfax!arnold Fax: +1 404 933 7606 | -- Miriam Hartholz