DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with awk

Command-line arguments

The command-line arguments are available to an awk program: the array ARGV contains the elements ARGV[0], . . ., ARGV[ARGC-1]; as in C, ARGC is the count. ARGV[0] is the name of the program (generally awk); the remaining arguments are whatever was provided (excluding the program and any optional arguments) when awk is invoked. The following command line contains an awk program that echoes the arguments that appear after the program name:

   awk '
   BEGIN {
      for (i = 1; i < ARGC; i++)
            printf "%s ", ARGV[i]
      printf "\n"
   }' $*
The arguments may be modified or added to; ARGC may be altered. As each input file ends, awk treats the next non-null element of ARGV (up to the current value of ARGC-1) as the name of the next input file.

One exception to the rule that an argument is a filename is when it is in the form

var=value

Then the variable var is set to the value value, as if by assignment. Such an argument is not treated like a filename. If value is a string, no quotes are needed.

A better way to initialize an awk variable on the command line is to use the -v option (-vvar=value) since there is no ambiguity.


Next topic: Using awk with other commands and the shell
Previous topic: getline function

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004