gawk: Other Arguments
1
1 2.3 Other Command-Line Arguments
1 ================================
1
1 Any additional arguments on the command line are normally treated as
1 input files to be processed in the order specified. However, an
1 argument that has the form 'VAR=VALUE', assigns the value VALUE to the
11 variable VAR--it does not specify a file at all. (See ⇒Assignment
Options.) In the following example, COUNT=1 is a variable assignment,
1 not a file name:
1
1 awk -f program.awk file1 count=1 file2
1
1 All the command-line arguments are made available to your 'awk'
1 program in the 'ARGV' array (⇒Built-in Variables). Command-line
1 options and the program text (if present) are omitted from 'ARGV'. All
1 other arguments, including variable assignments, are included. As each
1 element of 'ARGV' is processed, 'gawk' sets 'ARGIND' to the index in
1 'ARGV' of the current element.
1
1 Changing 'ARGC' and 'ARGV' in your 'awk' program lets you control how
1 'awk' processes the input files; this is described in more detail in
1 ⇒ARGC and ARGV.
1
1 The distinction between file name arguments and variable-assignment
1 arguments is made when 'awk' is about to open the next input file. At
1 that point in execution, it checks the file name to see whether it is
1 really a variable assignment; if so, 'awk' sets the variable instead of
1 reading a file.
1
1 Therefore, the variables actually receive the given values after all
1 previously specified files have been read. In particular, the values of
1 variables assigned in this fashion are _not_ available inside a 'BEGIN'
1 rule (⇒BEGIN/END), because such rules are run before 'awk' begins
1 scanning the argument list.
1
1 The variable values given on the command line are processed for
1 escape sequences (⇒Escape Sequences). (d.c.)
1
1 In some very early implementations of 'awk', when a variable
1 assignment occurred before any file names, the assignment would happen
1 _before_ the 'BEGIN' rule was executed. 'awk''s behavior was thus
1 inconsistent; some command-line assignments were available inside the
1 'BEGIN' rule, while others were not. Unfortunately, some applications
1 came to depend upon this "feature." When 'awk' was changed to be more
1 consistent, the '-v' option was added to accommodate applications that
1 depended upon the old behavior.
1
1 The variable assignment feature is most useful for assigning to
1 variables such as 'RS', 'OFS', and 'ORS', which control input and output
1 formats, before scanning the data files. It is also useful for
1 controlling state if multiple passes are needed over a data file. For
1 example:
1
1 awk 'pass == 1 { PASS 1 STUFF }
1 pass == 2 { PASS 2 STUFF }' pass=1 mydata pass=2 mydata
1
1 Given the variable assignment feature, the '-F' option for setting
1 the value of 'FS' is not strictly necessary. It remains for historical
1 compatibility.
1