• No results found

Variables and Fields

In document nawkA4 (Page 110-112)

awk variables are dynamic; they come into existence when they are first used. Their values are either floating-point numbers or strings. awkalso has one-dimension arrays; multiple-dimensional arrays may be simulated. There are several predefined variables thatawk sets as a program runs; these are summarized below.

A.3.1 Fields

As each input line is read, awk splits the line into fields, using the value of the FS variable as the field separator. If FS is a single character, fields are separated by that character. Otherwise,

FS is expected to be a full regular expression. In the special case that FS is a single blank, fields are separated by runs of blanks and/or tabs.

Each field in the input line may be referenced by its position,$1,$2, and so on. $0 is the whole line. The value of a field may be assigned to as well. Field numbers need not be constants:

n = 5 print $n

prints the fifth field in the input line. The variable NF is set to the total number of fields in the input line.

References to nonexistent fields (i.e., fields after $NF) return the null-string. However, assigning to a nonexistent field (e.g., $(NF+2) = 5) increases the value of NF, creates any intervening fields with the null string as their value, and causes the value of $0 to be recomputed, with the fields being separated by the value of OFS.

See Chapter 3 [Reading Input Files], page 21, for a full description of the wayawk defines and uses fields.

A.3.2 Built-in Variables

awk’s built-in variables are:

ARGC The number of command line arguments (not including options or the awk program itself).

Appendix A:awk Summary 109

ARGV The array of command line arguments. The array is indexed from 0 to ARGC − 1. Dynamically changing the contents of ARGV can control the files used for data.

CONVFMT The conversion format to use when converting numbers to strings.

ENVIRON An array containing the values of the environment variables. The array is indexed by variable name, each element being the value of that variable. Thus, the environment variable HOME would be inENVIRON["HOME"]. Its value might be ‘/u/close’.

Changing this array does not affect the environment seen by programs which awk

spawns via redirection or thesystemfunction.

Some operating systems do not have environment variables. The array ENVIRON is empty when running on these systems.

FILENAME The name of the current input file. If no files are specified on the command line, the value of FILENAMEis ‘-’.

FNR The input record number in the current input file.

FS The input field separator, a blank by default.

NF The number of fields in the current input record.

NR The total number of input records seen so far.

OFMT The output format for numbers for the print statement,"%.6g"by default.

OFS The output field separator, a blank by default.

ORS The output record separator, by default a newline.

RS The input record separator, by default a newline. RS is exceptional in that only the first character of its string value is used for separating records. If RSis set to the null string, then records are separated by blank lines. When RS is set to the null string, then the newline character always acts as a field separator, in addition to whatever value FSmay have.

RSTART The index of the first character matched by match; 0 if no match.

RLENGTH The length of the string matched by match;1 if no match.

SUBSEP The string used to separate multiple subscripts in array elements, by default"\034". See Chapter 13 [Built-in Variables], page 101, for more information.

A.3.3 Arrays

Arrays are subscripted with an expression between square brackets (‘[’ and ‘]’). Array subscripts

arealwaysstrings; numbers are converted to strings as necessary, following the standard conversion

rules (seeSection 8.9 [Conversion of Strings and Numbers], page 67).

If you use multiple expressions separated by commas inside the square brackets, then the array subscript is a string consisting of the concatenation of the individual subscript values, converted to strings, separated by the subscript separator (the value of SUBSEP).

The special operatorinmay be used in aniforwhilestatement to see if an array has an index consisting of a particular value.

if (val in array)

print array[val]

If the array has multiple subscripts, use(i, j,. . .) in arrayto test for existence of an element. The in construct may also be used in a for loop to iterate over all the elements of an array. SeeSection 10.5 [Scanning all Elements of an Array], page 84.

An element may be deleted from an array using the deletestatement. See Chapter 10 [Arrays inawk], page 81, for more detailed information.

A.3.4 Data Types

The value of an awkexpression is always either a number or a string.

Certain contexts (such as arithmetic operators) require numeric values. They convert strings to numbers by interpreting the text of the string as a numeral. If the string does not look like a numeral, it converts to 0.

Certain contexts (such as concatenation) require string values. They convert numbers to strings by effectively printing them with sprintf. See Section 8.9 [Conversion of Strings and Numbers], page 67, for the details.

To force conversion of a string value to a number, simply add 0 to it. If the value you start with is already a number, this does not change it.

To force conversion of a numeric value to a string, concatenate it with the null string.

The awklanguage defines comparisons as being done numerically if both operands are numeric, or if one is numeric and the other is a numeric string. Otherwise one or both operands are converted to strings and a string comparison is performed.

Uninitialized variables have the string value "" (the null, or empty, string). In contexts where a number is required, this is equivalent to 0.

SeeSection 8.2 [Variables], page 59, for more information on variable naming and initialization; seeSection 8.9 [Conversion of Strings and Numbers], page 67, for more information on how variable values are interpreted.

In document nawkA4 (Page 110-112)