manpagez: man pages & more
info gawk
Home | html | info | man

File: gawk.info,  Node: Other Arguments,  Next: Naming Standard Input,  Prev: Options,  Up: Invoking Gawk

2.3 Other Command-Line Arguments
================================

Any additional arguments on the command line are normally treated as
input files to be processed in the order specified.  However, an
argument that has the form 'VAR=VALUE', assigns the value VALUE to the
variable VAR--it does not specify a file at all.  (See *note Assignment
Options::.)  In the following example, 'count=1' is a variable
assignment, not a file name:

     awk -f program.awk file1 count=1 file2

As a side point, should you really need to have 'awk' process a file
named 'count=1' (or any file whose name looks like a variable
assignment), precede the file name with './', like so:

     awk -f program.awk file1 ./count=1 file2

   All the command-line arguments are made available to your 'awk'
program in the 'ARGV' array (*note Built-in Variables::).  Command-line
options and the program text (if present) are omitted from 'ARGV'.  All
other arguments, including variable assignments, are included.  As each
element of 'ARGV' is processed, 'gawk' sets 'ARGIND' to the index in
'ARGV' of the current element.  ('gawk' makes the full command line,
including program text and options, available in 'PROCINFO["argv"]';
*note Auto-set::.)

   Changing 'ARGC' and 'ARGV' in your 'awk' program lets you control how
'awk' processes the input files; this is described in more detail in
*note ARGC and ARGV::.

   The distinction between file name arguments and variable-assignment
arguments is made when 'awk' is about to open the next input file.  At
that point in execution, it checks the file name to see whether it is
really a variable assignment; if so, 'awk' sets the variable instead of
reading a file.

   Therefore, the variables actually receive the given values after all
previously specified files have been read.  In particular, the values of
variables assigned in this fashion are _not_ available inside a 'BEGIN'
rule (*note BEGIN/END::), because such rules are run before 'awk' begins
scanning the argument list.

   The variable values given on the command line are processed for
escape sequences (*note Escape Sequences::).  (d.c.)

   In some very early implementations of 'awk', when a variable
assignment occurred before any file names, the assignment would happen
_before_ the 'BEGIN' rule was executed.  'awk''s behavior was thus
inconsistent; some command-line assignments were available inside the
'BEGIN' rule, while others were not.  Unfortunately, some applications
came to depend upon this "feature."  When 'awk' was changed to be more
consistent, the '-v' option was added to accommodate applications that
depended upon the old behavior.

   The variable assignment feature is most useful for assigning to
variables such as 'RS', 'OFS', and 'ORS', which control input and output
formats, before scanning the data files.  It is also useful for
controlling state if multiple passes are needed over a data file.  For
example:

     awk 'pass == 1  { PASS 1 STUFF }
          pass == 2  { PASS 2 STUFF }' pass=1 mydata pass=2 mydata

   Given the variable assignment feature, the '-F' option for setting
the value of 'FS' is not strictly necessary.  It remains for historical
compatibility.

           Quoting Shell Variables On The 'awk' Command Line

   Small 'awk' programs are often embedded in larger shell scripts, so
it's worthwhile to understand some shell basics.  Consider the
following:

     f=""
     awk '{ print("hi") }' $f

   In this case, 'awk' reads from standard input instead of trying to
open any command line files.  To the unwary, this looks like 'awk' is
hanging.

   However 'awk' doesn't see an explicit empty string.  When a variable
expansion is the null string, _and_ it's not quoted, the shell simply
removes it from the command line.  To demonstrate:

     $ f=""
     $ awk 'BEGIN { print ARGC }' $f
     -| 1
     $ awk 'BEGIN { print ARGC }' "$f"
     -| 2

© manpagez.com 2000-2025
Individual documents may contain additional copyright information.