[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
7.5.2 Built-in Variables That Convey Information
The following is an alphabetical list of variables that awk
sets automatically on certain occasions in order to provide
information to your program. The variables that are specific to
gawk
are marked with a pound sign (‘#’).
-
ARGC, ARGV
The command-line arguments available to
awk
programs are stored in an array calledARGV
.ARGC
is the number of command-line arguments present. See section Other Command-Line Arguments. Unlike mostawk
arrays,ARGV
is indexed from 0 toARGC
- 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’, andARGV[2]
contains ‘BBS-list’. The value ofARGC
is three, one more than the index of the last element inARGV
, because the elements are numbered from zero.The names
ARGC
andARGV
, as well as the convention of indexing the array from 0 toARGC
- 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 inARGV
, nor are any ofawk
’s command-line options. See section UsingARGC
andARGV
, for information about howawk
uses these variables. (d.c.)-
ARGIND #
The index in
ARGV
of the current file being processed. Every timegawk
opens a new data file for processing, it setsARGIND
to the index inARGV
of the file name. Whengawk
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 yourawk
program,gawk
automatically sets it to a new value when the next file is opened.This variable is a
gawk
extension. In otherawk
implementations, or ifgawk
is in compatibility mode (see section Command-Line Options), it is not special.-
ENVIRON
An associative array containing the values of the environment. The array indices are the environment variable names; the elements are the values of the particular environment variables. For example,
ENVIRON["HOME"]
might be ‘/home/arnold’. Changing this array does not affect the environment passed on to any programs thatawk
may spawn via redirection or thesystem()
function.Some operating systems may not have environment variables. On such systems, the
ENVIRON
array is empty (except forENVIRON["AWKPATH"]
, see section TheAWKPATH
Environment Variable).-
ERRNO #
If a system error occurs during a redirection for
getline
, during a read forgetline
, or during aclose()
operation, thenERRNO
contains a string describing the error.In addition,
gawk
clearsERRNO
before opening each command-line input file. This enables checking if the file is readable inside aBEGINFILE
pattern (see section TheBEGINFILE
andENDFILE
Special Patterns).Otherwise,
ERRNO
works similarly to the C variableerrno
. Except for the case just mentioned,gawk
never clears it (sets it to zero or""
). Thus, you should only expect its value to be meaningful when an I/O operation returns a failure value, such asgetline
returning -1. You are, of course, free to clear it yourself before doing an I/O operation.This variable is a
gawk
extension. In otherawk
implementations, or ifgawk
is in compatibility mode (see section Command-Line Options), it is not special.-
FILENAME
The name of the file that
awk
is currently reading. When no data files are listed on the command line,awk
reads from the standard input andFILENAME
is set to"-"
.FILENAME
is changed each time a new file is read (see section Reading Input Files). Inside aBEGIN
rule, the value ofFILENAME
is""
, since there are no input files being processed yet.(36) (d.c.) Note, though, that usinggetline
(see section Explicit Input withgetline
) inside aBEGIN
rule can giveFILENAME
a value.-
FNR
The current record number in the current file.
FNR
is incremented each time a new record is read (see section How Input Is Split into Records). It is reinitialized to zero each time a new input file is started.-
NF
The number of fields in the current input record.
NF
is set each time a new record is read, when a new field is created or when$0
changes (see section Examining Fields).Unlike most of the variables described in this section, assigning a value to
NF
has the potential to affectawk
’s internal workings. In particular, assignments toNF
can be used to create or remove fields from the current record. See section Changing the Contents of a Field.-
NR
The number of input records
awk
has processed since the beginning of the program’s execution (see section How Input Is Split into Records).NR
is incremented each time a new record is read.-
PROCINFO #
The elements of this array provide access to information about the running
awk
program. The following elements (listed alphabetically) are guaranteed to be available:-
PROCINFO["egid"]
The value of the
getegid()
system call.-
PROCINFO["euid"]
The value of the
geteuid()
system call.-
PROCINFO["FS"]
This is
"FS"
if field splitting withFS
is in effect,"FIELDWIDTHS"
if field splitting withFIELDWIDTHS
is in effect, or"FPAT"
if field matching withFPAT
is in effect.-
PROCINFO["gid"]
The value of the
getgid()
system call.-
PROCINFO["pgrpid"]
The process group ID of the current process.
-
PROCINFO["pid"]
The process ID of the current process.
-
PROCINFO["ppid"]
The parent process ID of the current process.
-
PROCINFO["sorted_in"]
If this element exists in
PROCINFO
, its value controls the order in which array indices will be processed by ‘for (index in array) …’ loops. Since this is an advanced feature, we defer the full description until later; see Scanning All Elements of an Array.-
PROCINFO["strftime"]
The default time format string for
strftime()
. Assigning a new value to this element changes the default. See section Time Functions.-
PROCINFO["uid"]
The value of the
getuid()
system call.-
PROCINFO["version"]
The version of
gawk
.
On some systems, there may be elements in the array,
"group1"
through"groupN"
for some N. N is the number of supplementary groups that the process has. Use thein
operator to test for these elements (see section Referring to an Array Element).The
PROCINFO
array is also used to cause coprocesses to communicate over pseudo-ttys instead of through two-way pipes; this is discussed further in Two-Way Communications with Another Process.This array is a
gawk
extension. In otherawk
implementations, or ifgawk
is in compatibility mode (see section Command-Line Options), it is not special.-
-
RLENGTH
The length of the substring matched by the
match()
function (see section String-Manipulation Functions).RLENGTH
is set by invoking thematch()
function. Its value is the length of the matched string, or -1 if no match is found.-
RSTART
The start-index in characters of the substring that is matched by the
match()
function (see section String-Manipulation Functions).RSTART
is set by invoking thematch()
function. Its value is the position of the string where the matched substring starts, or zero if no match was found.-
RT #
This is set each time a record is read. It contains the input text that matched the text denoted by
RS
, the record separator.This variable is a
gawk
extension. In otherawk
implementations, or ifgawk
is in compatibility mode (see section Command-Line Options), it is not special.
Advanced Notes: Changing NR
and FNR
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.)
The following example shows this:
$ echo '1 > 2 > 3 > 4' | awk 'NR == 2 { NR = 17 } > { print NR }' -| 1 -| 17 -| 18 -| 19 |
Before FNR
was added to the awk
language
(see section Major Changes Between V7 and SVR3.1),
many awk
programs used this feature to track the number of
records in a file by resetting NR
to zero when FILENAME
changed.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |