Go to the previous, next section.
Most awk
variables are available for you to use for your own
purposes; they never change except when your program assigns them, and
never affect anything except when your program examines them.
A few variables have special built-in meanings. Some of them awk
examines automatically, so that they enable you to tell awk
how
to do certain things. Others are set automatically by awk
, so
that they carry information from the internal workings of awk
to
your program.
This chapter documents all the built-in variables of gawk
. Most
of them are also documented in the chapters where their areas of
activity are described.
awk
This is a list of the variables which you can change to control how
awk
does certain things.
FS
FS
is the input field separator (see section Specifying How Fields Are Separated).
The value is a single-character string or a multi-character regular
expression that matches the separations between fields in an input
record.
The default value is " "
, a string consisting of a single
space. As a special exception, this value actually means that any
sequence of spaces and tabs is a single separator. It also causes
spaces and tabs at the beginning or end of a line to be ignored.
You can set the value of FS
on the command line using the
`-F' option:
awk -F, 'program' input-files
IGNORECASE
IGNORECASE
is nonzero, then all regular expression
matching is done in a case-independent fashion. In particular, regexp
matching with `~' and `!~', and the gsub
index
,
match
, split
and sub
functions all ignore case when
doing their particular regexp operations. Note: since field
splitting with the value of the FS
variable is also a regular
expression operation, that too is done with case ignored.
See section Case-sensitivity in Matching.
If gawk
is in compatibility mode (see section Invocation of awk
), then
IGNORECASE
has no special meaning, and regexp operations are
always case-sensitive.
OFMT
awk
to control conversion of numbers to
strings (see section Conversion of Strings and Numbers). It works by being passed, in effect, as
the first argument to the sprintf
function. Its default value
is "%.6g"
.
OFS
print
statement. Its
default value is " "
, a string consisting of a single space.
ORS
print
statement. Its default value is a string containing a
single newline character, which could be written as "\n"
.
(See section Output Separators).
RS
awk
's record separator. Its default value is a string
containing a single newline character, which means that an input record
consists of a single line of text. (See section How Input is Split into Records.)
SUBSEP
SUBSEP
is a subscript separator. It has the default value of
"\034"
, and is used to separate the parts of the name of a
multi-dimensional array. Thus, if you access foo[12,3]
, it
really accesses foo["12\0343"]
. (See section Multi-dimensional Arrays).
This is a list of the variables that are set automatically by awk
on certain occasions so as to provide information for your program.
ARGC
ARGV
awk
are stored in an
array called ARGV
. ARGC
is the number of command-line
arguments present. ARGV
is indexed from zero to ARGC - 1
.
See section Invocation of awk
. For example:
awk '{ print ARGV[$1] }' inventory-shipped BBS-list
In this example, ARGV[0]
contains "awk"
, ARGV[1]
contains "inventory-shipped"
, and ARGV[2]
contains
"BBS-list"
. The value of ARGC
is 3, one more than the
index of the last element in ARGV
since the elements are numbered
from zero.
Notice that the awk
program is not entered in ARGV
. The
other special command line options, with their arguments, are also not
entered. But variable assignments on the command line are
treated as arguments, and do show up in the ARGV
array.
Your program can alter ARGC
and the elements of ARGV
.
Each time awk
reaches the end of an input file, it uses the next
element of ARGV
as the name of the next input file. By storing a
different string there, your program can change which files are read.
You can use "-"
to represent the standard input. By storing
additional elements and incrementing ARGC
you can cause
additional files to be read.
If you decrease the value of ARGC
, that eliminates input files
from the end of the list. By recording the old value of ARGC
elsewhere, your program can treat the eliminated arguments as
something other than file names.
To eliminate a file from the middle of the list, store the null string
(""
) into ARGV
in place of the file's name. As a
special feature, awk
ignores file names that have been
replaced with the null string.
ENVIRON
ENVIRON["HOME"]
might be `/u/close'. Changing this array
does not affect the environment passed on to any programs that
awk
may spawn via redirection or the system
function.
(In a future version of gawk
, it may do so.)
Some operating systems may not have environment variables.
On such systems, the array ENVIRON
is empty.
FILENAME
awk
is currently reading.
If awk
is reading from the standard input (in other words,
there are no files listed on the command line),
FILENAME
is set to "-"
.
FILENAME
is changed each time a new file is read (see section Reading Input Files).
FNR
FNR
is the current record number in the current file. FNR
is
incremented each time a new record is read (see section Explicit Input with getline
).
It is reinitialized to 0 each time a new input file is started.
NF
NF
is 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).
NR
awk
has processed since
the beginning of the program's execution. (see section How Input is Split into Records).
NR
is set each time a new record is read.
RLENGTH
RLENGTH
is the length of the substring matched by the
match
function (see section Built-in Functions for String Manipulation). RLENGTH
is set
by invoking the match
function. Its value is the length of the
matched string, or -1 if no match was found.
RSTART
RSTART
is the start-index of the substring matched by the
match
function (see section Built-in Functions for String Manipulation). RSTART
is set
by invoking the match
function. Its value is the position of the
string where the matched substring starts, or 0 if no match was
found.
Go to the previous, next section.