Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Close D.B.The AWK manual.1995.pdf
Источник:
Скачиваний:
7
Добавлен:
23.08.2013
Размер:
679.83 Кб
Скачать

Chapter 13: Built-in Variables

101

13 Built-in Variables

Most awk variables are available for you to use for your own purposes; they never change except when your program assigns values to them, and never a ect 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 awk. Most of them are also documented in the chapters where their areas of activity are described.

13.1 Built-in Variables that Control awk

This is a list of the variables which you can change to control how awk does certain things.

CONVFMT

This string is used by awk to control conversion of numbers to strings (see Section 8.9

 

[Conversion of Strings and Numbers], page 67). It works by being passed, in e ect, as

 

the rst argument to the sprintf function. Its default value is "%.6g". CONVFMT was

 

introduced by the posix standard.

FS

FS is the input eld separator (see Section 3.5 [Specifying how Fields are Separated],

 

page 25). The value is a single-character string or a multi-character regular expression

 

that matches the separations between elds 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' inputles

OFMT

This string is used by awk to control conversion of numbers to strings (see Section 8.9

 

[Conversion of Strings and Numbers], page 67) for printing with the print statement.

 

It works by being passed, in e ect, as the rst argument to the sprintf function. Its

 

default value is "%.6g". Earlier versions of awk also used OFMT to specify the format

 

for converting numbers to strings in general expressions; this has been taken over by

 

CONVFMT.

OFS

This is the output eld separator (see Section 4.3 [Output Separators], page 37). It

 

is output between the elds output by a print statement. Its default value is " ", a

 

string consisting of a single space.

ORS

This is the output record separator. It is output at the end of every print statement.

 

Its default value is a string containing a single newline character, which could be written

 

as "\n". (See Section 4.3 [Output Separators], page 37.)

RS

This is awk's input 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 3.1 [How Input is Split into Records], page 21.)

SUBSEP

SUBSEP is the 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

102

The AWK Manual

foo[12,3], it really accesses foo["12\0343"] (see Section 10.8 [Multi-dimensional Arrays], page 86).

13.2 Built-in Variables that Convey Information

This is a list of the variables that are set automatically by awk on certain occasions so as to provide information to your program.

ARGC

ARGV The command-line arguments available to awk programs are stored in an array called ARGV. ARGC is the number of command-line arguments present. See Chapter 14 [Invoking awk], page 105. ARGV is indexed from zero to ARGC - 1. For example:

awk 'BEGIN {

for (i = 0; i < ARGC; i++) print ARGV[i]

}' 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.

The names ARGC and ARGV, as well the convention of indexing the array from 0 to ARGC - 1, are derived from the C language's method of accessing command line arguments.

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 le, it uses the next element of ARGV as the name of the next inputle. By storing a di erent string there, your program can change which les are read. You can use "-" to represent the standard input. By storing additional elements and incrementing ARGC you can cause additional les to be read.

If you decrease the value of ARGC, that eliminates input les 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 le names.

To eliminate a le from the middle of the list, store the null string ("") into ARGV in place of the le's name. As a special feature, awk ignores le names that have been replaced with the null string.

ENVIRON This is an array that contains the values of the environment. The array indices are the environment variable names; the values are the values of the particular environment variables. For example, ENVIRON["HOME"] might be `/u/close'. Changing this array does not a ect the environment passed on to any programs that awk may spawn via redirection or the system function.

Some operating systems may not have environment variables. On such systems, the array ENVIRON is empty.

FILENAME This is the name of the le that awk is currently reading. If awk is reading from the standard input (in other words, there are no les listed on the command line), FILENAME is set to "-". FILENAME is changed each time a new le is read (see Chapter 3 [Reading Input Files], page 21).

Chapter 13: Built-in Variables

103

FNR

FNR is the current record number in the current le. FNR is incremented each time

 

a new record is read (see Section 3.7 [Explicit Input with getline], page 30). It is

 

reinitialized to 0 each time a new input le is started.

NF

NF is the number of elds in the current input record. NF is set each time a new record

 

is read, when a new eld is created, or when $0 changes (see Section 3.2 [Examining

 

Fields], page 22).

NR

This is the number of input records awk has processed since the beginning of the

 

program's execution. (see Section 3.1 [How Input is Split into Records], page 21). 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 11.3

 

[Built-in Functions for String Manipulation], page 90). 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 in characters of the substring matched by the match function

 

(see Section 11.3 [Built-in Functions for String Manipulation], page 90). 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.

104

The AWK Manual