[gnu.misc.discuss] Passing variables to gawk

mikemc@mustang.ncr-fc.FtCollins.NCR.com (Mike McManus) (01/18/90)

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 <var>=<value> 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?

Thanks!
--
Disclaimer: All spelling and/or grammer in this document are guaranteed to be
            correct; any exseptions is the is wurk uv intter-net deemuns.

Mike McManus (mikemc@ncr-fc.FtCollins.ncr.com)  
NCR Microelectronics                
2001 Danfield Ct.                   ncr-fc!mikemc@ncr-sd.sandiego.ncr.com, or
Ft. Collins,  Colorado              ncr-fc!mikemc@ccncsu.colostate.edu, or
(303) 223-5100   Ext. 360           uunet!ncrlnk!ncr-sd!ncr-fc!garage!mikemc
                                    

lwall@jato.Jpl.Nasa.Gov (Larry Wall) (01/19/90)

In article <MIKEMC.90Jan17141246@mustang.ncr-fc.FtCollins.NCR.com> 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 <var>=<value> 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?

I believe this is called "bug emulation".  awk itself has the same problem.

(It is possible that this was not considered a bug when first implemented.
I can imagine someone wishing to override the BEGIN section from the command
line.  Let us be charitable, and call it a misleading feature  :-)

I chose not to emulate this particular misleading feature in the awk-to-perl
translator.  The produced perl script evaluates the foo=bar switches before
doing the "BEGIN" stuff.  If you want the other behavior, you can just move
the line that does that, which says

    eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;

down below the stuff derived from BEGIN.

Oddly enough, in the version of nawk I have access to, it is documented
to have the buggy behavior, but, in fact, works right.  Go figure.

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov

maart@cs.vu.nl (Maarten Litmaath) (01/19/90)

In article <MIKEMC.90Jan17141246@mustang.ncr-fc.FtCollins.NCR.com>,
	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 <var>=<value> instead of file
\names:  [...]

[and it doesn't work]

Known disgusting bug: the variables aren't set until the FIRST LINE HAS BEEN
READ.  Workaround:

	NR > 1 {
		# do something with lines > 1
		next;
	}
	NR == 1 {
		# do something with command line variables
		# do something with line 1
	}

(In this example either the `next;' statement or the `NR == 1' test can be
left out.)
-- 
  What do the following have in common:  access(2), SysV echo, O_NONDELAY?  |
  Maarten Litmaath @ VU Amsterdam:  maart@cs.vu.nl,  uunet!mcsun!botter!maart

arnold@audiofax.com (Arnold Robbins) (01/20/90)

In article <MIKEMC.90Jan17141246@mustang.ncr-fc.FtCollins.NCR.com> 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 <var>=<value> 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