Go to the first, previous, next, last section, table of contents.
The following is an alphabetical list of variables that @command{awk} sets automatically on certain occasions in order to provide information to your program. The variables that are specific to @command{gawk} are marked with an asterisk (`*').
ARGC, ARGV
ARGV. ARGC is the number of command-line
arguments present. See section Other Command-Line Arguments.
Unlike most @command{awk} arrays,
ARGV is indexed from 0 to ARGC - 1.
In the following example:
$ awk 'BEGIN {
> for (i = 0; i < ARGC; i++)
> print ARGV[i]
> }' inventory-shipped BBS-list
-| awk
-| inventory-shipped
-| BBS-list
ARGV[0] contains "awk", ARGV[1]
contains "inventory-shipped" and ARGV[2] contains
"BBS-list". The value of ARGC is three, one more than the
index of the last element in ARGV, because the elements are numbered
from zero.
The names ARGC and ARGV, as well as the convention of indexing
the array from 0 to ARGC - 1, are derived from the C language's
method of accessing command-line arguments.
The value of ARGV[0] can vary from system to system.
Also, you should note that the program text is not included in
ARGV, nor are any of @command{awk}'s command-line options.
See section Using ARGC and ARGV, for information
about how @command{awk} uses these variables.
ARGIND #
ARGV of the current file being processed.
Every time @command{gawk} opens a new data file for processing, it sets
ARGIND to the index in ARGV of the file name.
When @command{gawk} is processing the input files,
`FILENAME == ARGV[ARGIND]' is always true.
This variable is useful in file processing; it allows you to tell how far
along you are in the list of data files as well as to distinguish between
successive instances of the same file name on the command line.
While you can change the value of ARGIND within your @command{awk}
program, @command{gawk} automatically sets it to a new value when the
next file is opened.
This variable is a @command{gawk} extension.
In other @command{awk} implementations,
or if @command{gawk} is in compatibility mode
(see section Command-Line Options),
it is not special.
ENVIRON
ENVIRON["HOME"] might be `/home/arnold'. Changing this array
does not affect the environment passed on to any programs that
@command{awk} may spawn via redirection or the system function.
Some operating systems may not have environment variables.
On such systems, the ENVIRON array is empty (except for
ENVIRON["AWKPATH"],
@pxref{AWKPATH Variable, ,The @env{AWKPATH} Environment Variable}).
ERRNO #
getline,
during a read for getline, or during a close operation,
then ERRNO contains a string describing the error.
This variable is a @command{gawk} extension.
In other @command{awk} implementations,
or if @command{gawk} is in compatibility mode
(see section Command-Line Options),
it is not special.
FILENAME
FILENAME is set to "-".
FILENAME is changed each time a new file is read
(see section Reading Input Files).
Inside a BEGIN rule, the value of FILENAME is
"", since there are no input files being processed
yet.(25) initialized
FILENAME to "-", even if there were data files to be
processed. This behavior was incorrect and should not be relied
upon in your programs.}
(d.c.)
Note though, that using getline
(see section Explicit Input with getline)
inside a BEGIN rule can give
FILENAME a value.
FNR
FNR is
incremented each time a new record is read
(see section Explicit Input with getline). It is reinitialized
to zero each time a new input file is started.
NF
NF is set each time a new record is read, when a new field is
created or when $0 changes (see section Examining Fields).
NR
NR is incremented each time a new record is read.
PROCINFO #
PROCINFO["egid"]
getegid system call.
PROCINFO["euid"]
geteuid system call.
PROCINFO["FS"]
"FS" if field splitting with FS is in effect, or it is
"FIELDWIDTHS" if field splitting with FIELDWIDTHS is in effect.
PROCINFO["gid"]
getgid system call.
PROCINFO["pgrpid"]
PROCINFO["pid"]
PROCINFO["ppid"]
PROCINFO["uid"]
getuid system call.
"group1"
through "groupN" for some N. N is the number of
supplementary groups that the process has. Use the in operator
to test for these elements
(see section Referring to an Array Element).
This array is a @command{gawk} extension.
In other @command{awk} implementations,
or if @command{gawk} is in compatibility mode
(see section Command-Line Options),
it is not special.
RLENGTH
match function
(see section String Manipulation Functions).
RLENGTH is set by invoking the match function. Its value
is the length of the matched string, or -1 if no match is found.
RSTART
match function
(see section String Manipulation Functions).
RSTART is set by invoking the match function. Its value
is the position of the string where the matched substring starts, or zero
if no match was found.
RT #
RS, the record separator.
This variable is a @command{gawk} extension.
In other @command{awk} implementations,
or if @command{gawk} is in compatibility mode
(see section Command-Line Options),
it is not special.
NR and FNR
@command{awk} increments NR and FNR
each time it reads a record, instead of setting them to the absolute
value of the number of records read. This means that a program can
change these variables and their new values are incremented for
each record.
(d.c.)
This is demonstrated in the following example:
$ echo '1
> 2
> 3
> 4' | awk 'NR == 2 { NR = 17 }
> { print NR }'
-| 1
-| 17
-| 18
-| 19
Before FNR was added to the @command{awk} language
(see section Major Changes Between V7 and SVR3.1),
many @command{awk} programs used this feature to track the number of
records in a file by resetting NR to zero when FILENAME
changed.
Go to the first, previous, next, last section, table of contents.