Assigning Variables on the Command Line
---------------------------------------
You can set any `awk' variable by including a "variable assignment"
among the arguments on the command line when you invoke `awk' (
see Invoking `awk': Command Line.). Such an assignment has this form:
VARIABLE=TEXT
With it, you can set a variable either at the beginning of the `awk'
run or in between input files.
If you precede the assignment with the `-v' option, like this:
-v VARIABLE=TEXT
then the variable is set at the very beginning, before even the `BEGIN'
rules are run. The `-v' option and its assignment must precede all the
file name arguments, as well as the program text.
Otherwise, the variable assignment is performed at a time determined
by its position among the input file arguments: after the processing of
the preceding input file argument. For example:
awk '{ print $n }' n=4 inventory-shipped n=2 BBS-list
prints the value of field number `n' for all input records. Before the
first file is read, the command line sets the variable `n' equal to 4.
This causes the fourth field to be printed in lines from the file
`inventory-shipped'. After the first file has finished, but before the
second file is started, `n' is set to 2, so that the second field is
printed in lines from `BBS-list'.
Command line arguments are made available for explicit examination by
the `awk' program in an array named `ARGV' (*note Built-in
Variables::.).
`awk' processes the values of command line assignments for escape
sequences (see Constant Expressions: Constants.).