awk - Process programs written in the awk language

Format

awk [-F ere] [-v var=value ...] [program] [var=value ...] [file ...]

awk [-F ere] [-f prog] [-v var=value ...] [var=value ...] [file ...]

Description

awk is a file-processing language that is well suited to data manipulation and retrieval of information from text files. If you are unfamiliar with the language, you might find it helpful to read information in awk in z/OS UNIX System Services User's Guide first.

An awk program consists of any number of user-defined functions and rules of the form:
pattern {action}
There are two ways to specify the awk program:
  • Directly on the command line. In this case, program is a single command-line argument, typically enclosed in single quotation marks (') to prevent the shell from attempting to expand it.
  • By using the -f prog option.

You can specify program directly on the command line only if you do not use any -f prog arguments.

For a summary of the UNIX03 changes to this command, see Shell commands changed for UNIX03.

Options

awk recognizes the following options:
-F ere
Is an extended regular expression to use as the field separator.
-f prog
Runs the awk program contained in the file prog. When more than one -f option appears on the command line, the resulting program is a concatenation of all programs you specify.
-v var=value
Assigns value to var before running the program.

Files that you specify on the command line with the file argument provide the input data for awk to manipulate. If you specify no files or you specify a dash as a file, awk reads data from standard input (stdin).

You can initialize variables on the command line using:
var=value
You can intersperse such initializations with the names of input files on the command line. awk processes initializations and input files in the order they appear on the command line. For example, the command:
awk -f progfile a=1 f1 f2 a=2 f3

sets a to 1 before reading input from f1 and sets a to 2 before reading input from f3.

Variable initializations that appear before the first file on the command line are performed immediately after the BEGIN action. Initializations appearing after the last file are performed immediately before the END action. For more information about BEGIN and END, see Patterns.

Use the -v option to assign a value to a variable before the awk program begins execution (that is, before the BEGIN action). For example, in:
awk -v v1=10 -f prog datafile

awk assigns the variable v1 its value before the BEGIN action of the program (but after default assignments made to such built-in variables as FS and OFMT; these built-in variables have special meaning to awk).

awk divides input into records. By default, newline characters separate records; however, you can specify a different record separator if you want.

One at a time, and in order, awk compares each input record with the pattern of every rule in the program. When a pattern matches, awk performs the action part of the rule on that input record. Patterns and actions often refer to separate fields within a record. By default, white space (usually blanks, newlines, or horizontal tab characters) separates fields; however, you can specify a different field separator string using the -F ere option).

You can omit the pattern or action part of an awk rule (but not both). If you omit pattern, awk performs the action on every input record (that is, every record matches). If you omit action, awk's default action is equivalent to: {print}.

awk considers everything after a # in a program line to be a comment. For example:
# This is a comment

To continue program lines on the next line, add a backslash (\) to the end of the line. Statement lines ending with a comma (,), double or-bars (||), or double ampersands (&&) continue automatically on the next line.

Variables and expressions

There are three types of variables in awk: identifiers, fields, and array elements.

An identifier is a sequence of letters, digits, and underscores beginning with a letter or an underscore. These characters must be from the POSIX portable character set. (Data can come from other character sets.)

For a description of fields, see Input.

Arrays are associative collections of values called the elements of the array. Constructs of the form:
identifier[subscript]

where subscript has the form expr or expr,expr,..., refer to array elements. Each such expr can have any string value. For multiple expr subscripts, awk concatenates the string values of all expr arguments with a separate character SUBSEP between each. The initial value of SUBSEP is set to \042 (code page 01047 field separator).

Fields and identifiers are sometimes referred to as scalar variables to distinguish them from arrays.

You do not declare awk variables, and you do not need to initialize them. The value of an uninitialized variable is the empty string in a string context and the number 0 in a numeric context.

Expressions consist of constants, variables, functions, regular expressions, and subscript-in-array conditions combined with operators. (Subscript-in-array conditions are described in Subxcript in array.) Each variable and expression has a string value and a corresponding numeric value; awk uses the value appropriate to the context.

When converting a numeric value to its corresponding string value, awk performs the equivalent of a call to the sprintf() function where the one and only expr argument is the numeric value and the fmt argument is either %d (if the numeric value is an integer) or the value of the variable CONVFMT (if the numeric value is not an integer). The default value of CONVFMT is %.6g. If you use a string in a numeric context, and awk cannot interpret the contents of the string as a number, it treats the value of the string as zero.

Numeric constants are sequences of decimal digits.

String constants are quoted, as in "a literal string". Literal strings can contain the following escape sequences:
Escape character Sequence
\a Audible bell
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\ooo Octal value ooo
\xdd Hexadecimal value dd
\/ Slash
\" Quote
\c Any other character c
awk supports full regular expressions. (See Regular expressions (regexp) for more information.) When awk reads a program, it compiles characters that are enclosed in slash characters (/) as regular expressions. In addition, when literal strings and variables appear on the right side of a ~ or!~ operator, or as certain arguments to built-in matching and substitution functions, awk interprets them as dynamic regular expressions.
Note: When you use literal strings as regular expressions, you need extra backslashes to escape regular expression metacharacters, because the backslash is also the literal string escape character. For example, the regular expression:
/e\.g\./
should be written as:
"e\\.g\\."

Subscript in array

awk defines the subscript-in-array condition as:

index in array

where index looks like expr or (expr,...,expr). This condition evaluates to 1 if the string value of index is a subscript of array, and to 0 otherwise. This is a way to determine if an array element exists. When the element does not exist, the subscript-in-array condition does not create it.

Symbol table

You can access the symbol table through the built-in array SYMTAB. SYMTAB[expr] is equivalent to the variable named by the evaluation of expr.

For example, SYMTAB["var"] is a synonym for the variable var.

Environment

An awk program can determine its initial environment by examining the ENVIRON array. If the environment consists of entries of the form name=value, then ENVIRON[name] has string value "value". For example, the following program is equivalent to the default output of env:
BEGIN {        for (i in ENVIRON)
                printf("%s=%s\n", i, ENVIRON[i])
        exit
     }

Operators

awk follows the usual precedence order of arithmetic operations, unless overridden with parentheses; a table giving the order of operations appears later in this topic.

The unary operators are +, -, ++, and - -, where you can use the ++ and - - operators as either postfix or prefix operators, as in C. The binary arithmetic operators are +, -, *, /, %, and ^.

The conditional operator
expr ? expr1 : expr2
evaluates to expr1 if the value of expr is nonzero, and to expr2 otherwise.

If two expressions are not separated by an operator, awk concatenates their string values.

The tilde operator ~ yields 1 (true) if the regular expression on the right side matches the string on the left side. The operator !~ yields 1 when the right side has no match on the left. To illustrate:
$2 ~ /[0-9]/

selects any line where the second field contains at least one digit. awk interprets any string or variable on the right side of ~ or !~ as a dynamic regular expression.

The relational operators are <, <=, >, >=, ==, and !=. When both operands in a comparison are numeric, or if one is numeric and the other is not initialized, awk compares their values numerically; otherwise, it compares them as strings. An operator is considered to be numeric if it consists of any of the following:
  • An integer or floating-point number.
  • A field, FILENAME, ARGV array element, or ENVIRON array element that looks like a number.
  • A variable created by a command-line assignment that looks like a number.
  • Input from a getline() function that looks like a number.
  • An array element created by the split() function that looks like a number.
  • A variable assignment from another number variable that looks like a number.

The Boolean operators are || (or), && (and), and ! (not). awk uses short-circuit evaluation when evaluating expressions. With an && expression, if the first operator is false, the entire expression is false and it is not necessary to evaluate the second operator. With an || expression, a similar situation exists if the first operator is true.

You can assign values to a variable with:
var = expr
If op is a binary arithmetic operator, var op= expr is equivalent to var = var op expr, except that var is evaluated only once.

For the order of operations for awk, refer to the awk documentation in z/OS UNIX System Services Command Reference.

Command-line arguments

awk sets the built-in variable ARGC to the number of command-line arguments. The built-in array ARGV has elements subscripted with digits from zero to ARGC-1, giving command-line arguments in the order they appeared on the command line.

The ARGC count and the ARGV vector do not include command-line options (beginning with -) or the program file (following -f). They do include the name of the command itself, initialization statements of the form var=value, and the names of input data files.

awk creates ARGC and ARGV before doing anything else. It then walks through ARGV, processing the arguments. If an element of ARGV is an empty string, awk skips it. If it contains an equals sign (=), awk interprets it as a variable assignment. If it is a minus sign (-), awk immediately reads input from stdin until it encounters the end of the file. Otherwise, awk treats the argument as a file name and reads input from that file until it reaches the end of the file. awk runs the program by walking through ARGV in this way. Thus, if the program changes ARGV, awk can read different files and make different assignments.

Input

awk divides input into records. A record separator character separates each record from the next. The value of the built-in variable RS gives the current record separator character; by default, it begins as the newline (\n). If you assign a different character to RS, awk uses that as the record separator character from that point on.

awk divides records into fields. A field separator string, given by the value of the built-in variable FS, separates each field from the next. You can set a specific separator string by assigning a value to FS, or by specifying the -F ere option on the command line. You can assign a regular expression to FS. For example:
FS = "[,:$]"
says that commas, colons, or dollar signs can separate fields. As a special case, assigning FS a string that contains only a blank character sets the field separator to white space. In this case, awk considers any sequence of contiguous space or tab characters a single field separator. This is the default for FS. However, if you assign FS a string containing any other character, that character designates the start of a new field. For example, if we set FS=\t (the tab character),
texta \t textb \t  \t  \t textc
contains five fields, two of which contain only blanks. With the default setting, this record only contains three fields, since awk considers the sequence of multiple blanks and tabs a single separator.
The following list of built-in variables provides various pieces of information about input:
NF
Number of fields in the current record
NR
Number of records read so far
FILENAME
Name of file that contains the current record
FNR
Number of records that was read from current file

Field specifiers have the form $n, where n runs from 1 through NF. Such a field specifier refers to the nth field of the current input record. $0 (zero) refers to the entire current input record.

The getline function can read a value for a variable or $0 from the current input, from a file, or from a pipe. The result of getline is an integer indicating whether the read operation was successful. A value of 1 indicates success; 0 indicates that the end of the file was encountered; and -1 indicates that an error occurred. Possible forms for getline are:
getline
Reads next input record into $0 and splits the record into fields. NF, NR, and FNR are set appropriately.
getline var
Reads the next input record into the variable var. awk does not split the record into fields (which means that the current $n values do not change), but sets NR and FNR appropriately.
getline <expr
Interprets the string value of expr to be a file name. awk reads the next record from that file into $0, splits it into fields, and sets NF appropriately. If the file is not open, awk opens it. The file remains open until you close it with a close function.
getline var <expr
Interprets the string value of expr to be a file name, and reads the next record from that file into the variable var, but does not split it into fields.
expr | getline
Interprets the string value of expr as a command line to be run. awk pipes output from this command into getline, and reads it into $0, splits it into fields, and sets NF appropriately. See System function for additional details.
expr | getline var
Runs the string value of expr as a command and pipes the output of the command into getline. The result is similar to getline var <expr.

You can have only a limited number of files and pipes open at one time. You can close files and pipes during execution using the close(expr) function. The expr argument must be one that came before | or after < in getline, or after > or >> in print or printf.

If the function successfully closes the pipe, it returns zero. By closing files and pipes that you no longer need, you can use any number of files and pipes in the course of running an awk program.

Built-in arithmetic functions

atan2(expr1, expr2)
Returns the arctangent of expr1/expr2 in the range of minus pi through pi.
exp(expr), log(expr), sqrt(expr)
Returns the exponential, natural logarithm, and square root of the numeric value of expr. If you omit (expr), these functions use $0 instead.
int(expr)
Returns the integer part of the numeric value of expr. If you omit (expr), the function returns the integer part of $0.
rand()
Returns a random floating-point number in the range 0 through 1.
sin(expr), cos(expr)
Returns the sine and cosine of the numeric value of expr (interpreted as an angle in radians).
srand(expr)
Sets the seed of the rand function to the integer value of expr. If you omit (expr), awk uses the time of day as a default seed.

Built-in string functions

len = length (expr)
Returns the number of characters in the string value of expr. If you omit (expr), the function uses $0 instead. The parentheses around expr are optional.
n = split(string, array, regexp)
Splits the string into fields. regexp is a regular expression giving the field separator string for the purposes of this operation. This function assigns the separate fields, in order, to the elements of array; subscripts for array begin at 1. awk discards all other elements of array. split returns the number of fields into which it divided string (which is also the maximum subscript for array).

regexp divides the record in the same way that the FS field separator string does. If you omit regexp in the call to split, it uses the current value of FS.

str = substr(string, offset, len)
Returns the substring of string that begins in position offset and is at most len characters long. The first character of the string has an offset of 1. If you omit len, or if len specifies more characters than are left in the string, substr returns the rest of string.
pos = index(string, str)
Returns the position of the first occurrence of str in string. The count is in characters. If index does not find str in string, it returns 0.
pos = match(string, regexp)
Searches string for the first substring matching the regular expression regexp, and returns an integer giving the position of this substring counting from 1. If it finds no such substring, match returns zero. This function also sets the built-in variable RSTART to pos and the built-in variable RLENGTH to the length of the matched string. If it does not find a match, match sets RSTART to 0, and RLENGTH to -1. You can enclose regexp in slashes or specify it as a string.
n = sub(regexp, repl, string)
Searches string for the first substring matching the regular expression regexp, and replaces the substring with the string repl. awk replaces any ampersand (&) in repl with the substring of string which matches regexp. An ampersand preceded with a backslash ('\') is interpreted as the literal ampersand character. An occurrence of two consecutive backslashes is interpreted as just a single literal backslash character. Any other occurrence of a backslash (for example, preceding any other character) is treated as a literal backslash character. If repl is a string literal, the handling of the ampersand character occurs after any lexical processing, including any lexical backslash escape sequence. If you omit string, sub uses the current record instead. sub returns the number of substrings replaced (which is 1 if it found a match, and 0 otherwise).
n = gsub(regexp, repl, string)
Works the same way as sub, except that gsub replaces all matching substrings (global substitution). The return value is the number of substitutions performed.
str = sprintf(fmt, expr, expr...)
Formats the expression list expr, expr, ... using specifications from the string fmt, and then returns the formatted string. The fmt string consists of conversion specifications that convert and add the next expr to the string, and ordinary characters that sprintf simply adds to the string. These conversion specifications are similar to those used by the American National Standard C standard.
Conversion specifications have the form
 %[-][0][x][.y]c
where
-
Left-justifies the field; default is right justification.
0
(Leading zero) prints numbers with leading zero.
x
Is the minimum field width.
y
Is the precision.
c
Is the conversion character.

In a string, the precision is the maximum number of characters to be printed from the string; in a number, the precision is the number of digits to be printed to the right of the decimal point in a floating-point value. If x or y is * (asterisk), the minimum field width or precision is the value of the next expr in the call to sprintf.

The conversion character c is one of following:
d
Decimal integer.
i
Decimal integer.
o
Unsigned octal integer.
x,X
Unsigned hexadecimal integer.
u
Unsigned decimal integer.
f,F
Floating point.
e,E
Floating point (scientific notation).
g,G
The shorter of e and f (suppresses nonsignificant zeros).
c
Single character of an integer value; first character of string.
s
String.
The lowercase x specifies alphabetic hexadecimal digits in lowercase, whereas the uppercase X specifies alphabetic hexadecimal digits in uppercase. The other uppercase-lowercase pairs work similarly.
n = ord(expr)
Returns the integer value of first character in the string value of expr. This is useful in conjunction with %c in sprintf.
str = tolower(expr)
Converts all letters in the string value of expr into lowercase, and returns the result. If you omit expr, tolower uses $0 instead. This function uses the value of the locale or the LC_CTYPE environment variable.
str = toupper(expr)
Converts all letters in the string value of expr into uppercase, and returns the result. If you omit expr, toupper uses $0 instead. This function uses the value of the locale or the LC_CTYPE environment variable.

System function

status = system(expr)
Runs the string value of expr as a command. For example, system("tail " $1) calls the tail command, using the string value of $1 as the file that tail examines. The standard command interpreter runs the command, as discussed in the Portability section, and the exit status that is returned depends on that command interpreter.

User-defined functions

You can define your own functions using the form:
function name(parameter-list) {
        statements
}

A function definition can appear in the place of a pattern {action} rule. The parameter-list argument contains any number of normal (scalar) and array variables that are separated by commas. When you call a function, awk passes scalar arguments by value, and array arguments by reference. The names specified in parameter-list are local to the function; all other names that are used in the function are global. You can define local variables by adding them to the end of the parameter list as long as no call to the function uses these extra parameters.

A function returns to its caller either when it runs the final statement in the function, or when it reaches an explicit return statement. The return value, if any, is specified in the return statement (see Actions).

Patterns

A pattern is a regular expression, a special pattern, a pattern range, or any arithmetic expression.

BEGIN is a special pattern used to label actions that awk performs before reading any input records. END is a special pattern that is used to label actions that awk performs after reading all input records.

You can give a pattern range as:
pattern1,pattern2

This matches all lines from one that matches pattern1 to one that matches pattern2, inclusive.

If you omit a pattern, or if the numeric value of the pattern is nonzero (true), awk runs the resulting action for the line.

Actions

An action is a series of statements ended by semicolons, newlines, or closing braces. A condition is any expression; awk considers a nonzero value true, and a zero value false. A statement is one of the following or any series of statements enclosed in braces:
# expression statement, for example, assignment
expression
# if statement
if (condition)
        statement
[else
        statement]
# while loop
while (condition)
        statement
# do-while loop
do
        statement
while (condition)
# for loop
for (expression1; condition; expression2)
        statement
The for statement is equivalent to:
expression1
while (condition) {
        statement
        expression2
}
The for statement can also have the form:
for (i in array)
        statement
awk runs the statement (specified with the statement argument) once for each element in array; on each repetition, the variable i contains the name of a subscript of array, running through all the subscripts in an arbitrary order. If array is multidimensional (has multiple subscripts), i is expressed as a single string with the SUBSEP character separating the subscripts.
  • The statement break exits a for or a while loop immediately. continue stops the current iteration of a for or while loop and begins the next iteration (if there is one).
  • next ends any processing for the current input record and immediately starts processing the next input record. Processing for the next record begins with the first appropriate rule. If a next statement appears or is invoked in a BEGIN or END action, awk will cause all further BEGIN or END action processing to be abandoned.
  • exit[(expr)] immediately goes to the END action if it exists; if there is no END action, or if awk is already running the END action, the awk program ends. awk sets the exit status of the program to the numeric value of expr. If you omit (expr), the exit status is 0. return [expr] returns from the execution of a function.

If you specify an expr, the function returns the value of the expression as its result; otherwise, the function result is undefined. delete array[i] deletes element i from the given array. print expr, expr, ... is described in Output. printf fmt, expr, expr, ... is also described in Output.

Output

The print statement prints its arguments with only simple formatting. If it has no arguments, it prints the entire current input record. awk adds the output record separator ORS to the end of the output that each print statement produces; when commas separate arguments in the print statement, the output field separator OFS separates the corresponding output values. ORS and OFS are built-in variables, whose values you can change by assigning them strings. The default output record separator is a newline, and the default output field separator is a space.

The variable OFMT gives the format of floating-point numbers output by print. By default, the value is %.6g; you can change this by assigning OFMT a different string value. OFMT applies only to floating-point numbers (ones with fractional parts).

The printf statement formats its arguments using the fmt argument. Formatting is the same as for the built-in function sprintf. Unlike print, printf does not add output separators automatically. This gives the program more precise control of the output.

The print and printf statements write to stdout. You can redirect output to a file or pipe.

If you add >expr to a print or printf statement, awk treats the string value of expr as a file name, and writes output to that file. Similarly, if you add >>expr, awk sends output to the current contents of the file. The distinction between > and >> is important only for the first print to the file expr. Subsequent outputs to an already open file append to what is there already.

You cannot use such ambiguous statements as:
print a > b c

Use parentheses to resolve the ambiguity.

If you add |expr to a print or printf statement, awk treats the string value of expr as an executable command and runs it with the output from the statement piped as input into the command.

As a reminder, you can have only a limited number of files and pipes open at any time. To avoid going over the limit, use the close function to close files and pipes when you no longer need them.

print and printf are also available as functions with the same calling sequence, but no redirection.

Examples

  1. The following example:
    awk '{print NR ":" $0}' input1
    outputs the contents of the file input1 with line numbers that are prepended to each line.
  2. The following is an example using var=value on the command line:
    awk '{print NR SEP $0}' SEP=":" input1
    awk can also read the program script from a file as in the command line:
    awk -f addline.awk input1
    which produces the same output when the file addline.awk contains:
    {print NR ":" $0}
  3. The following program appends all input lines that start with January to the file jan (which might or might not exist already), and all lines that start with February or March to the file febmar:
    /^January/ {print >> "jan"}
    /^February|^March/ {print >> "febmar"}
  4. This program prints the total and average for the last column of each input line:
            {s += $NF}
    END     {print "sum is", s, "average is", s/NR}
  5. The next program interchanges the first and second fields of input lines:
    {        tmp = $1
            $1 = $2
            $2 = tmp
            print
    }
  6. The following inserts line numbers so that output lines are left-aligned:
    {printf "%-6d: %s\n", NR, $0}
  7. The following prints input records in reverse order (assuming sufficient memory):
    {
            a[NR] = $0 # index using record number
    }
    END {
            for (i = NR; i>0; --i)
                    print a[i]
    }
  8. The following program determines the number of lines that start with the same first field:
    {
            ++a[$1] # array indexed using the first field
    }
    END {   # note output will be in undefined order
            for (i in a)
                    print a[i], "lines start with", i
    }
    You can use the following program to determine the number of lines in each input file:
    {
            ++a[FILENAME]
    }
    END {
            for (file in a)
                    if (a[file] == 1)
                            print file, "has 1 line"
                    else
                            print file, "has", a[file], "lines"
    }
  9. The following program illustrates how you can use a two-dimensional array in awk. Assume that the first field of each input record contains a product number, the second field contains a month number, and the third field contains a quantity (bought, sold, or whatever). The program generates a table of products versus month.
    BEGIN   {NUMPROD = 5}
    {
            array[$1,$2] += $3
    }
    END     {
            print "\t Jan\t Feb\tMarch\tApril\t May\t" \
                "June\tJuly\t Aug\tSept\t Oct\t Nov\t Dec"
            for (prod = 1; prod <= NUMPROD; prod++) {
                    printf "%-7s", "prod#" prod
                    for (month = 1; month <= 12; month++){
                            printf "\t%5d", array[prod,month]
                    }
                    printf "\n"
            }
    }
  10. As the following program reads in each line of input, it reports whether the line matches a predetermined value:
    function randint() {
            return (int((rand()+1)*10))
    }
    BEGIN   {
            prize[randint(),randint()] = "$100";
            prize[randint(),randint()] = "$10";
            prize[1,1] = "the booby prize"
            }
    {
            if (($1,$2) in prize)
                    printf "You have won %s!\n", prize[$1,$2]
    }
  11. The following example prints lines, the first and last fields of which are the same, reversing the order of the fields:
    $1==$NF {
            for (i = NF; i > 0; --i)
                    printf "%s", $i (i>1 ? OFS : ORS)
    }
  12. The following program prints the input files from the command line. The infiles function first empties the passed array, and then fills the array. The extra parameter i of infiles is a local variable.
    function infiles(f,i) {
            for (i in f)
                   delete f[i]
            for (i = 1; i < ARGC; i++)
                    if (index(ARGV[i],"=") == 0)
                            f[i] = ARGV[i]
    }
    BEGIN   {
            infiles(a)
            for (i in a)
                    print a[i]
            exit
         }
  13. Here is the standard recursive factorial function:
    function fact(num) {
            if (num <= 1)
                    return 1
            else
                    return num * fact(num - 1)
    }
    { print $0 " factorial is " fact($0) }
  14. The following program illustrates the use of getline with a pipe. Here, getline sets the current record from the output of the wc command. The program prints the number of words in each input file.
    function words(file,   string) {
            string = "wc " fn
            string | getline
            close(string)
            return ($2)
    }
    BEGIN   {
            for (i=1; i<ARGC; i++) {
                    fn = ARGV[i]
                    printf "There are %d words in %s.",
                        words(fn), fn
            }
    }

Environment variables

awk uses the following environment variables:
PATH
Contains a list of directories that awk searches when looking for commands run by system(expr), or input and output pipes.
_UNIX03
For more information about the effect of _UNIX03 on the awk command, see Shell commands changed for UNIX03.

Any other environment variable can be accessed by the awk program itself.

Localization

awk uses the following localization environment variables:
  • LANG
  • LC_ALL
  • LC_COLLATE
  • LC_CTYPE
  • LC_MESSAGES
  • LC_NUMERIC
  • LC_SYNTAX
  • NLSPATH

Exit values

0
Successful completion
  • If the awk program contains no actions and no patterns, but is otherwise a valid awk program, standard input and any file operands are not read and awk exits with an exit status of zero.
1
Any of the following errors:
  • Parser internal stack overflow
  • Syntax error
  • Function redefined
  • Internal execution tree error
  • Insufficient memory for string storage
  • Unbalanced parenthesis or brace
  • Missing script file
  • Missing field separator
  • Missing variable assignment
  • Unknown option
  • Incorrect character in input
  • Newline in regular expression
  • Newline in string
  • EOF in regular expression
  • EOF in string
  • Cannot open script file
  • Inadmissible use of reserved keyword
  • Attempt to redefine built-in function
  • Cannot open input file
  • Error on print
  • Error on printf
  • Getline in END action was not redirected
  • Too many open I/O streams
  • Error on I/O stream
  • Insufficient arguments to printf or sprintf()
  • Array cannot be used as a scalar
  • Variable cannot be used as a function
  • Too many fields
  • Record too long
  • Division (/ or %) by zero
  • Syntax error
  • Cannot assign to a function
  • Value required in assignment
  • Return outside of a function
  • Can delete only array element or array
  • Scalar cannot be used as array
  • SYMTAB must have exactly one index
  • Impossible function call
  • Function call nesting level exceeded
  • Wrong number of arguments to function
  • Regular expression error
  • Second parameter to split must be an array
  • sprintf string longer than allowed number of characters
  • No open file name
  • Function requires an array
  • Is not a function
  • Failed to match
  • Incorrect collation element
  • Trailing \ in pattern
  • Newline found before end of pattern
  • More than 9 \( \) pairs
  • Number in [0-9] incorrect
  • [ ] imbalance or syntax error
  • ( ) or \( \) imbalance
  • { } or \{ \} imbalance
  • Incorrect endpoint in range
  • Out of memory
  • Incorrect repetition
  • Incorrect character class type
  • Internal error
  • Unknown regex error

When an awk program ends because of a call to exit(), the exit status is the value that is passed to exit().

Limits

Most constructions in this implementation of awk are dynamic, limited only by memory restrictions of the system.

The maximum record size is guaranteed to be at least LINE_MAX as returned by getconf. The maximum field size is guaranteed to be LINE_MAX, also.

The parser stack depth is limited to 150 levels. Attempting to process extremely complicated programs might result in an overflow of this stack, causing an error.

Input must be text files.

Portability

POSIX.2, X/Open Portability Guide UNIX systems.

The ord function is an extension to traditional implementations of awk. The toupper and tolower functions and the ENVIRON array are in POSIX and the UNIX System V Release 4 version of awk. This version is a superset of New awk, as described in The AWK Programming Language by Aho, Weinberger, and Kernighan.

The standard command interpreter that the system function uses and that awk uses to run pipelines for getline, print, and printf is system-dependent. On z/OS UNIX, this interpreter is always /bin/sh.

Related information

ed, egrep, sed, vi

For more information about regexp, see Regular expressions (regexp).