gawk: Assignment Options

1 
1 6.1.3.2 Assigning Variables on the Command Line
1 ...............................................
1 
1 Any 'awk' variable can be set by including a "variable assignment" among
11 the arguments on the command line when 'awk' is invoked (⇒Other
 Arguments).  Such an assignment has the following form:
1 
1      VARIABLE=TEXT
1 
1 With it, a variable is set either at the beginning of the 'awk' run or
1 in between input files.  When the assignment is preceded with the '-v'
1 option, as in the following:
1 
1      -v VARIABLE=TEXT
1 
1 the variable is set at the very beginning, even before the 'BEGIN' rules
1 execute.  The '-v' option and its assignment must precede all the file
1 name arguments, as well as the program text.  (⇒Options for more
1 information about the '-v' option.)  Otherwise, the variable assignment
1 is performed at a time determined by its position among the input file
1 arguments--after the processing of the preceding input file argument.
1 For example:
1 
1      awk '{ print $n }' n=4 inventory-shipped n=2 mail-list
1 
1 prints the value of field number 'n' for all input records.  Before the
1 first file is read, the command line sets the variable 'n' equal to
1 four.  This causes the fourth field to be printed in lines from
1 'inventory-shipped'.  After the first file has finished, but before the
1 second file is started, 'n' is set to two, so that the second field is
1 printed in lines from 'mail-list':
1 
1      $ awk '{ print $n }' n=4 inventory-shipped n=2 mail-list
1      -| 15
1      -| 24
1      ...
1      -| 555-5553
1      -| 555-3412
1      ...
1 
1    Command-line arguments are made available for explicit examination by
1 the 'awk' program in the 'ARGV' array (⇒ARGC and ARGV).  'awk'
1 processes the values of command-line assignments for escape sequences
1 (⇒Escape Sequences).  (d.c.)
1