-->

Previous | Table of Contents | Next

Page 162

_W compat, __compat Run in compatibility mode. In compatibility mode, gawk behaves identically to awk; none of the GNU-specific extensions are recognized. See "GNU Extensions," later in this manual page, for more information.
_W copyleft, _W copyright, __copyleft, __copyright Print the short version of the GNU copyright information message on the error output.
_W help, _W usage Print a relatively short summary of the available options on the error output. Per the GNU
__help, __usage Coding Standards, these options cause an immediate, successful exit.
_W lint, __lint Provide warnings about constructs that are dubious or nonportable to other awk implementations.
_W posix, __posix This turns on compatibility mode, with the following additional restrictions:\x escape sequences are not recognized.
The synonym func for the keyword function is not recognized.
The operators ** and **= cannot be used in place of ^ and ^=.
_W source=program-text, Use program-text as awk program source code. This option allows the easy intermixing of
__source=program-text library functions (used via the _f and __file options) with source code entered on the command line. It is intended primarily for medium to large awk programs used in shell scripts.
The _W source= form of this option uses the rest of the command-line argument for program-text; no other options to _W will be recognized in the same argument.
_W version, __version Print version information for this particular copy of gawk on the error output. This is useful mainly for knowing if the current copy of gawk on your system is up-to-date with respect to whatever the Free Software Foundation is distributing. Per the GNU Coding Standards, these options cause an immediate, successful exit.
__ Signal the end of options. This is useful to allow further arguments to the awk program itself to start with a _. This is mainly for consistency with the argument-parsing convention used by most other programs.

In compatibility mode, any other options are flagged as illegal, but are otherwise ignored. In normal operation, as long as program text has been supplied, unknown options are passed on to the awk program in the ARGV array for processing. This is particularly useful for running awk programs via the #! executable interpreter mechanism.

AWK PROGRAM EXECUTION

An awk program consists of a sequence of pattern-action statements and optional function definitions:


pattern { action statements }

function name(parameter list) { statements }

gawk first reads the program source from the program-file(s) if specified, from arguments to _W source=, or from the first nonoption argument on the command line. The _f and _W source= options may be used multiple times on the command line. gawk will read the program text as if all the program-files and command-line source texts had been concatenated together. This is useful for building libraries of awk functions, without having to include them in each new awk program that uses them. It also provides the ability to mix library functions with command-line programs.

The environment variable AWKPATH specifies a search path to use when finding source files named with the _f option. If this variable does not exist, the default path is .:/usr/lib/awk:/usr/local/lib/awk.

If a filename given to the _f option contains a / character, no path search is performed.

gawk executes awk programs in the following order. First, all variable assignments specified via the _v option are performed. Next, gawk compiles the program into an internal form. Then, gawk executes the code in the BEGIN block(s) (if any), and then proceeds to read each file named in the ARGV array. If there are no files named on the command line, gawk reads the standard input.

If a filename on the command line has the form var=val, it is treated as a variable assignment. The variable var will be assigned the value val. (This happens after any BEGIN block(s) have been run.) Command-line variable assignment is most

Page 163

useful for dynamically assigning values to the variables awk uses to control how input is broken into fields and records. It is also useful for controlling state if multiple passes are needed over a single data file.

If the value of a particular element of ARGV is empty (""), gawk skips over it.

For each line in the input, gawk tests to see if it matches any pattern in the awk program. For each pattern that the line matches, the associated action is executed. The patterns are tested in the order they occur in the program.

Finally, after all the input is exhausted, gawk executes the code in the END block(s) (if any).

VARIABLES AND FIELDS

awk variables are dynamic; they come into existence when they are first used. Their values are either floating-point numbers or strings, or both, depending upon how they are used. awk also has one-dimensional arrays; arrays with multiple dimensions may be simulated. Several predefined variables are set as a program runs; these will be described as needed and summarized in the "Built-In Variables" subsection.

FIELDS

As each input line is read, gawk 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 or tabs. Note that the value of IGNORECASE (see the following) will also affect how fields are split when FS is a regular expression.

If the FIELDWIDTHS variable is set to a space separated list of numbers, each field is expected to have fixed width, and gawk will split up the record using the specified widths. The value of FS is ignored. Assigning a new value to FS overrides the use of FIELDWIDTHS, and restores the default behavior.

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. Fields need not be referenced by 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 (that is, fields after $NF) produce the null-string. However, assigning to a nonexistent field (for example, $(NF+2) = 5) will increase the value of NF, create any intervening fields with the null string as their value, and cause the value of $0 to be recomputed, with the fields being separated by the value of OFS. References to negative-numbered fields cause a fatal error.

BUILT-IN VARIABLES

awk's built-in variables are the following:

ARGC The number of command-line arguments (does not include options to gawk, or the program source).
ARGIND The index in ARGV of the current file being processed.
ARGV 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 for numbers, "%.6g", by default.
ENVIRON An array containing the values of the current environment. The array is indexed by the environment variables, each element being the value of that variable (for example, ENVIRON["HOME"] might be /u/arnold). Changing this array does not affect the environment seen by programs which gawk spawns via redirection or the system() function. (This may change in a future version of gawk.)
ERRNO If a system error occurs either doing a redirection for getline, during a read for getline, or during a close(), then ERRNO will contain a string describing the error.
FIELDWIDTHS A whitespace-separated list of fieldwidths. When set, gawk parses the input into fields of fixed width, instead of using the value of the FS variable as the field separator. The fixed field width facility is still experimental; expect the semantics to change as gawk evolves over time.

Page 164

FILENAME The name of the current input file. If no files are specified on the command line, the value of FILENAME is _. However, FILENAME is undefined inside the BEGIN block.
FNR The input record number in the current input file.
FS The input field separator, a blank by default.
IGNORECASE Controls the case-sensitivity of all regular expression operations. If IGNORECASE has a nonzero value, then pattern matching in rules, field splitting with FS, regular expression matching with ~ and !~, and the gsub(), index(), match(), split(),and sub() predefined functions will all ignore case when doing regular expression operations. Thus, if IGNORECASE is not equal to zero, /aB/ matches all of the strings ab, aB, Ab, and AB. As with all awk variables, the initial value of IGNORECASE is zero, so all regular expression operations are normally case-sensitive.
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, "%.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. (This will probably change in a future release of gawk.) If RS is 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 FS may 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 character used to separate multiple subscripts in array elements, by default n034.

ARRAYS

Arrays are subscripted with an expression between square brackets. If the expression is an expression list (expr, expr ...) then the array subscript is a string consisting of the concatenation of the (string) value of each expression, separated by the value of the SUBSEP variable. This facility is used to simulate multiply dimensioned arrays. For example,


i = "A" ; j = "B" ; k = "C"

x[i, j, k] = "hello, world\n"

assigns the string hello, world\n to the element of the array x which is indexed by the string "A\034B\034C". All arrays in awk are associative, that is indexed by string values.

The special operator in may be used in an if or while statement 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 array.

The in construct may also be used in a for loop to iterate over all the elements of an array.

An element may be deleted from an array using the delete statement. The delete statement may also be used to delete the entire contents of an array.

VARIABLE TYPING AND CONVERSION

Variables and fields may be floating-point numbers, or strings, or both. How the value of a variable is interpreted depends upon its context. If used in a numeric expression, it will be treated as a number; if used as a string, it will be treated as a string.

To force a variable to be treated as a number, add 0 to it; to force it to be treated as a string, concatenate it with the null string.

Previous | Table of Contents | Next