AWK(1POSIX)
โจ AWK(1POSIX) โ POSIX Programmer's Manual โจ
๐ธ PROLOG ๐ธ
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
๐ NAME ๐
awk โ pattern scanning and processing language
๐ฆ SYNOPSIS ๐ฆ
awk [-F sepstring] [-v assignment]... program [argument...]
awk [-F sepstring] -f progfile [-f progfile]... [-v assignment]...
[argument...]
๐ DESCRIPTION ๐
okay so the awk utility shall execute programs written in the awk programming language, which is specialized for textual data manipulation. An awk program is a sequence of patterns and corresponding actions. When input is read that matches a pattern, the action associated with that pattern is carried out.
Input shall be interpreted as a sequence of records. By default, a record is a line, less its terminating <newline>, but this can be changed by using the RS built-in variable. Each record of input shall be matched in turn against each pattern in the program. For each pattern matched, the associated action shall be executed.
The awk utility shall interpret each input record as a sequence of fields where, by default, a field is a string of non-<blank> non-<newline> characters. This default <blank> and <newline> field delimiter can be changed by using the FS built-in variable or the -F sepstring option. The awk utility shall denote the first field in a record $1, the second $2, and so on. The symbol $0 shall refer to the entire record; setting any other field causes the re-evaluation of $0. Assigning to $0 shall reset the values of all other fields and the NF built-in variable.
โจ OPTIONS โจ
The awk utility shall conform to the Base Definitions volume of POSIX.1-2017, Section 12.2, Utility Syntax Guidelines.
The following options shall be supported:
-F sepstringโ Define the input field separator. This option shall be equivalent to:-v FS=sepstringexcept that if-F sepstringand-v FS=sepstringare both used, it is unspecified whether theFSassignment resulting from-F sepstringis processed in command line order or is processed after the last-v FS=sepstring. See the description of theFSbuilt-in variable, and how it is used, in the EXTENDED DESCRIPTION section.-f progfileโ Specify the pathname of the file progfile containing anawkprogram. A pathname of'-'shall denote the standard input. If multiple instances of this option are specified, the concatenation of the files specified as progfile in the order specified shall be theawkprogram. Theawkprogram can alternatively be specified in the command line as a single argument.-v assignmentโ The application shall ensure that the assignment argument is in the same form as an assignment operand. The specified variable assignment shall occur prior to executing theawkprogram, including the actions associated withBEGINpatterns (if any). Multiple occurrences of this option can be specified.
๐ OPERANDS ๐
The following operands shall be supported:
programโ If no-foption is specified, the first operand toawkshall be the text of theawkprogram. The application shall supply the program operand as a single argument toawk. If the text does not end in a<newline>,awkshall interpret the text as if it did.argumentโ Either of the following two types of argument can be intermixed:fileโ A pathname of a file that contains the input to be read, which is matched against the set of patterns in the program. If no file operands are specified, or if a file operand is'-', the standard input shall be used.assignmentโ An operand that begins with an<underscore>or alphabetic character from the portable character set (see the table in the Base Definitions volume of POSIX.1-2017, Section 6.1, Portable Character Set), followed by a sequence of underscores, digits, and alphabetics from the portable character set, followed by the'='character, shall specify a variable assignment rather than a pathname. The characters before the'='represent the name of anawkvariable; if that name is anawkreserved word (see Grammar) the behavior is undefined. The characters following the<equals-sign>shall be interpreted as if they appeared in theawkprogram preceded and followed by a double-quote ('"') character, as aSTRINGtoken (see Grammar), except that if the last character is an unescaped<backslash>, it shall be interpreted as a literal<backslash>rather than as the first character of the sequence """. The variable shall be assigned the value of thatSTRINGtoken and, if appropriate, shall be considered a numeric string (see Expressions in awk), the variable shall also be assigned its numeric value. Each such variable assignment shall occur just prior to the processing of the following file, if any. Thus, an assignment before the first file argument shall be executed after theBEGINactions (if any), while an assignment after the last file argument shall occur before theENDactions (if any). If there are no file arguments, assignments shall be executed before processing the standard input.
๐ซถ STDIN ๐ซถ
The standard input shall be used only if no file operands are specified, or if a file operand is '-', or if a progfile option-argument is '-'; see the INPUT FILES section. If the awk program contains no actions and no patterns, but is otherwise a valid awk program, standard input and any file operands shall not be read and awk shall exit with a return status of zero.
๐ INPUT FILES ๐
Input files to the awk program from any of the following sources shall be text files:
- Any file operands or their equivalents, achieved by modifying the
awkvariablesARGVandARGC - Standard input in the absence of any file operands
- Arguments to the
getlinefunction
Whether the variable RS is set to a value other than a <newline> or not, for these files, implementations shall support records terminated with the specified separator up to {LINE_MAX} bytes and may support longer records.
If -f progfile is specified, the application shall ensure that the files named by each of the progfile option-arguments are text files and their concatenation, in the same order as they appear in the arguments, is an awk program.
๐ ENVIRONMENT VARIABLES ๐
The following environment variables shall affect the execution of awk:
LANGโ Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1-2017, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)LC_ALLโ If set to a non-empty string value, override the values of all the other internationalization variables.LC_COLLATEโ Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating elements within regular expressions and in comparisons of string values.LC_CTYPEโ Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multi-byte characters in arguments and input files), the behavior of character classes within regular expressions, the identification of characters as letters, and the mapping of uppercase and lowercase characters for thetoupperandtolowerfunctions.LC_MESSAGESโ Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.LC_NUMERICโ Determine the radix character used when interpreting numeric input, performing conversions between numeric and string values, and formatting numeric output. Regardless of locale, the<period>character (the decimal-point character of the POSIX locale) is the decimal-point character recognized in processingawkprograms (including assignments in command line arguments).NLSPATHโ Determine the location of message catalogs for the processing ofLC_MESSAGES.PATHโ Determine the search path when looking for commands executed bysystem(expr), or input and output pipes; see the Base Definitions volume of POSIX.1-2017, Chapter 8, Environment Variables.
In addition, all environment variables shall be visible via the awk variable ENVIRON.
โญ ASYNCHRONOUS EVENTS โญ
Default.
๐ STDOUT ๐
The nature of the output files depends on the awk program.
๐ STDERR ๐
The standard error shall be used only for diagnostic messages.
๐ธ OUTPUT FILES ๐ธ
The nature of the output files depends on the awk program.
๐ EXTENDED DESCRIPTION ๐
Overall Program Structure
An awk program is composed of pairs of the form:
pattern { action }
Either the pattern or the action (including the enclosing brace characters) can be omitted.
A missing pattern shall match any record of input, and a missing action shall be equivalent to:
{ print }
That's the skeleton, bestie. ๐
okay so bestie, here's how the actual run goes down ๐ซ The awk program kicks off by first running the actions tied to all the BEGIN patterns, in the order they show up in the program. Then every file operand gets processed one at a time โ or standard input if you didn't name any files โ reading data from the file until a record separator shows up (<newline> by default, iykyk). And before the very first reference to a field in the record gets evaluated, that record gets split into fields following the rules in Regular Expressions, using whatever value of FS was current at the time the record was read. After that, every pattern in the program is evaluated in order of occurrence, and the action attached to each pattern that matches the current record runs. Matching action first, then the next patterns get evaluated. And finally? The actions tied to all the END patterns run, in the order they appear in the program. ๐
โจ Expressions in awk โจ
Expressions are literally the little computations that live inside your patterns and actions. ๐งฎ In the table below, valid expression operations are grouped from highest precedence first down to lowest precedence last, and operators that share the same precedence are grouped together. When the grammar is being formally ambiguous, higher precedence operators get evaluated before lower precedence ones โ no debate, that's just the rule. ๐
In this table, expr, expr1, expr2, and expr3 stand for any expression, while lvalue stands for anything that can be assigned to (that is, anything sitting on the left side of an assignment operator). The precise syntax of expressions lives in Grammar.
Table 4-1: Expressions in Decreasing Precedence in awk ๐
( expr )โ Grouping โ result type: Type of expr โ associativity: N/A$exprโ Field reference โ result type: String โ associativity: N/Alvalue ++โ Post-increment โ result type: Numeric โ associativity: N/Alvalue --โ Post-decrement โ result type: Numeric โ associativity: N/A++ lvalueโ Pre-increment โ result type: Numeric โ associativity: N/A-- lvalueโ Pre-decrement โ result type: Numeric โ associativity: N/Aexpr ^ exprโ Exponentiation โ result type: Numeric โ associativity: Right! exprโ Logical not โ result type: Numeric โ associativity: N/A+ exprโ Unary plus โ result type: Numeric โ associativity: N/A- exprโ Unary minus โ result type: Numeric โ associativity: N/Aexpr * exprโ Multiplication โ result type: Numeric โ associativity: Leftexpr / exprโ Division โ result type: Numeric โ associativity: Leftexpr % exprโ Modulus โ result type: Numeric โ associativity: Leftexpr + exprโ Addition โ result type: Numeric โ associativity: Leftexpr - exprโ Subtraction โ result type: Numeric โ associativity: Leftexpr exprโ String concatenation โ result type: String โ associativity: Leftexpr < exprโ Less than โ result type: Numeric โ associativity: Noneexpr <= exprโ Less than or equal to โ result type: Numeric โ associativity: Noneexpr != exprโ Not equal to โ result type: Numeric โ associativity: Noneexpr == exprโ Equal to โ result type: Numeric โ associativity: Noneexpr > exprโ Greater than โ result type: Numeric โ associativity: Noneexpr >= exprโ Greater than or equal to โ result type: Numeric โ associativity: Noneexpr ~ exprโ ERE match โ result type: Numeric โ associativity: Noneexpr !~ exprโ ERE non-match โ result type: Numeric โ associativity: Noneexpr in arrayโ Array membership โ result type: Numeric โ associativity: Left( index ) in arrayโ Multi-dimension array membership โ result type: Numeric โ associativity: Leftexpr && exprโ Logical AND โ result type: Numeric โ associativity: Leftexpr || exprโ Logical OR โ result type: Numeric โ associativity: Leftexpr1 ? expr2 : expr3โ Conditional expression โ result type: Type of selected expr2 or expr3 โ associativity: Rightlvalue ^= exprโ Exponentiation assignment โ result type: Numeric โ associativity: Rightlvalue %= exprโ Modulus assignment โ result type: Numeric โ associativity: Rightlvalue *= exprโ Multiplication assignment โ result type: Numeric โ associativity: Rightlvalue /= exprโ Division assignment โ result type: Numeric โ associativity: Rightlvalue += exprโ Addition assignment โ result type: Numeric โ associativity: Rightlvalue -= exprโ Subtraction assignment โ result type: Numeric โ associativity: Rightlvalue = exprโ Assignment โ result type: Type of expr โ associativity: Right
quick vibe check: the groups above are separated exactly where the original table draws its horizontal lines, so highest precedence is at the top and lowest is at the bottom. ๐ซถ
Every expression has either a string value, a numeric value, or both. Except where it's specifically stated for particular contexts, the value of an expression gets implicitly converted to whatever type the context it's used in needs. ๐
A string value gets converted to a numeric value either by the equivalent of these calls to functions defined by the ISO C standard:
setlocale(LC_NUMERIC, "");
numeric_value = atof(string_value);
or by converting the initial portion of the string to type double representation, like this:
- The input string is decomposed into two parts: an initial, possibly empty, sequence of white-space characters (as specified by
isspace()) and a subject sequence interpreted as a floating-point constant. โ๏ธ - The expected form of the subject sequence is an optional
'+'or'-'sign, then a non-empty sequence of digits optionally containing a<period>, then an optional exponent part. An exponent part consists of'e'or'E', followed by an optional sign, followed by one or more decimal digits. ๐ข - The sequence starting with the first digit or the
<period>(whichever occurs first) is interpreted as a floating constant of the C language, and if neither an exponent part nor a<period>appears, a<period>is assumed to follow the last digit in the string. If the subject sequence begins with a<hyphen-minus>, the value resulting from the conversion is negated. โ
heads up bestie: those two conversion routes are the exact ones the spec allows โ don't go inventing a third one, it's giving undefined behaviour energy. ๐
A numeric value that's exactly equal to an integer (peep Section 1.1.2, Concepts Derived from the ISO C Standard) gets turned into a string by the equivalent of calling sprintf with "%d" as the fmt argument and the numeric value as the one and only expr argument. Slay. Any other numeric value gets stringified by the equivalent of calling sprintf with the value of the variable CONVFMT as the fmt argument and the numeric value as the first and only expr argument. ๐ซถ
Heads up bestie: the result of that conversion is unspecified if the value of CONVFMT isn't a floating-point format specification. So don't do that. ๐
This volume of POSIX.1-2017 specifies no explicit conversions between numbers and strings. But lowkey you have workarounds: an application can force an expression to be treated as a number by adding zero to it, or can force it to be treated as a string by concatenating the null string ("") to it. Iconic behavior honestly. โจ
A string value shall be considered a numeric string if it comes from one of the following:
- Field variables
- Input from the
getline()function FILENAMEARGVarray elementsENVIRONarray elements- Array elements created by the
split()function - A command line variable assignment
- Variable assignment from another numeric string variable
โฆand an implementation-dependent condition corresponding to either case (a) or (b) below is met.
a. After the equivalent of the following calls to functions defined by the ISO C standard, string_value_end would differ from string_value, and any characters before the terminating null character in string_value_end would be <blank> characters:
char *string_value_end;
setlocale(LC_NUMERIC, "");
numeric_value = strtod (string_value, &string_value_end);
b. After all the following conversions have been applied, the resulting string would lexically be recognized as a NUMBER token as described by the lexical conventions in Grammar:
- All leading and trailing
<blank>characters are discarded. - If the first non-
<blank>is'+'or'-', it is discarded. - Each occurrence of the decimal point character from the current locale is changed to a
<period>.
In case (a) the numeric value of the numeric string shall be the value that would be returned by the strtod() call. In case (b), if the first non-<blank> is '-', the numeric value of the numeric string shall be the negation of the numeric value of the recognized NUMBER token; otherwise, the numeric value of the numeric string shall be the numeric value of the recognized NUMBER token. Whether or not a string is a numeric string shall be relevant only in contexts where that term is used in this section. ๐
When an expression is used in a Boolean context, if it has a numeric value, a value of zero shall be treated as false and any other value shall be treated as true. Otherwise, a string value of the null string shall be treated as false and any other value shall be treated as true. A Boolean context shall be one of the following:
- The first subexpression of a conditional expression
- An expression operated on by logical NOT, logical AND, or logical OR
- The second expression of a
forstatement - The expression of an
ifstatement - The expression of the
whileclause in either awhileordo...whilestatement - An expression used as a pattern (as in Overall Program Structure)
All arithmetic shall follow the semantics of floating-point arithmetic as specified by the ISO C standard (see Section 1.1.2, Concepts Derived from the ISO C Standard).
The value of the expression:
expr1 ^ expr2
shall be equivalent to the value returned by the ISO C standard function call:
pow(expr1, expr2)
The expression:
lvalue ^= expr
shall be equivalent to the ISO C standard expression:
lvalue = pow(lvalue, expr)
except that lvalue shall be evaluated only once. ๐
The value of the expression:
expr1 % expr2
shall be equivalent to the value returned by the ISO C standard function call:
fmod(expr1, expr2)
The expression:
lvalue %= expr
shall be equivalent to the ISO C standard expression:
lvalue = fmod(lvalue, expr)
except that lvalue shall be evaluated only once. (Yes, again. It's a whole thing. ๐ )
Variables and fields shall be set by the assignment statement:
lvalue = expression
and the type of expression shall determine the resulting variable type. The assignment includes the arithmetic assignments ("+=", "-=", "*=", "/=", "%=", "^=", "++", "--") all of which shall produce a numeric result. The left-hand side of an assignment and the target of increment and decrement operators can be one of a variable, an array with index, or a field selector. ๐ฆ
The awk language supplies arrays that are used for storing numbers or strings. Arrays need not be declared. They shall initially be empty, and their sizes shall change dynamically. The subscripts, or element identifiers, are strings, providing a type of associative array capability. An array name followed by a subscript within square brackets can be used as an lvalue and thus as an expression, as described in the grammar; see Grammar. Unsubscripted array names can be used in only the following contexts:
- A parameter in a function definition or function call
- The
NAMEtoken following any use of the keywordinas specified in the grammar (see Grammar); if the name used in this context is not an array name, the behavior is undefined ๐ญ
A valid array index shall consist of one or more <comma>-separated expressions, similar to the way in which multi-dimensional arrays are indexed in some programming languages. Because awk arrays are really one-dimensional, such a <comma>-separated list shall be converted to a single string by concatenating the string values of the separate expressions, each separated from the other by the value of the SUBSEP variable. Thus, the following two index operations shall be equivalent:
var[expr1, expr2, ... exprn]
var[expr1 SUBSEP expr2 SUBSEP ... SUBSEP exprn]
The application shall ensure that a multi-dimensioned index used with the in operator is parenthesized. The in operator, which tests for the existence of a particular array element, shall not cause that element to exist. Any other reference to a nonexistent array element shall automatically create it. (Not me gaslighting my own arrays ๐ญ)
Comparisons (with the '<', "<=", "!=", "==", '>', and ">=" operators) shall be made numerically if both operands are numeric, if one is numeric and the other has a string value that is a numeric string, or if one is numeric and the other has the uninitialized value. Otherwise, operands shall be converted to strings as required and a string comparison shall be made as follows:
- For the
"!="and"=="operators, the strings should be compared to check if they are identical but may be compared using the locale-specific collation sequence to check if they collate equally. - For the other operators, the strings shall be compared using the locale-specific collation sequence.
The value of the comparison expression shall be 1 if the relation is true, or 0 if the relation is false. Simple math, bestie. โญ
๐ธ Variables and Special Variables ๐ธ
Variables can be used in an awk program by referencing them. With the exception of function parameters (see User-Defined Functions), they are not explicitly declared. Function parameter names shall be local to the function; all other variable names shall be global. The same name shall not be used as both a function parameter name and as the name of a function or a special awk variable. The same name shall not be used both as a variable name with global scope and as the name of a function. The same name shall not be used within the same scope both as a scalar variable and as an array. ๐ซ
Uninitialized variables, including scalar variables, array elements, and field variables, shall have an uninitialized value. An uninitialized value shall have both a numeric value of zero and a string value of the empty string. Evaluation of variables with an uninitialized value, to either string or numeric, shall be determined by the context in which they are used. She contains multitudes fr fr. ๐
Field variables shall be designated by a '$' followed by a number or numerical expression. The effect of the field number expression evaluating to anything other than a non-negative integer is unspecified; uninitialized variables or string values need not be converted to numeric values in this context. New field variables can be created by assigning a value to them. References to nonexistent fields (that is, fields after $NF), shall evaluate to the uninitialized value. Such references shall not create new fields. However, assigning to a nonexistent field (for example, $(NF+2)=5) shall increase the value of NF; create any intervening fields with the uninitialized value; and cause the value of $0 to be recomputed, with the fields being separated by the value of OFS. ๐ซง
Each field variable shall have a string value or an uninitialized value when created. Field variables shall have the uninitialized value when created from $0 using FS and the variable does not contain any characters. If appropriate, the field variable shall be considered a numeric string (see Expressions in awk).
Implementations shall support the following other special variables that are set by awk:
ARGCโ The number of elements in theARGVarray.ARGVโ An array of command line arguments, excluding options and the program argument, numbered from zero toARGC-1.
The arguments in ARGV can be modified or added to; ARGC can be altered. As each input file ends, awk shall treat the next non-null element of ARGV, up to the current value of ARGC-1, inclusive, as the name of the next input file. Thus, setting an element of ARGV to null means that it shall not be treated as an input file. The name '-' indicates the standard input. If an argument matches the format of an assignment operand, this argument shall be treated as an assignment rather than a file argument.
CONVFMTโ Theprintfformat for converting numbers to strings (except for output statements, whereOFMTis used);"%.6g"by default.ENVIRONโ the entire environment, sitting right there as an array</p> ๐ซถ exactly the way the exec functions in the System Interfaces volume of POSIX.1โ2017 describe it. The indices are strings made of the names of the environment variables, and every array element's value is a string made of that variable's value. If it's appropriate, the environment variable counts as a numeric string (peep Expressions in awk); the array element also carries its numeric value.
In literally every case where awk's behavior is affected by environment variables โ including the environment of any command awk runs via the system function, or via pipeline redirections with the print statement, the printf statement, or the getline function โ the environment used is the one that existed when awk began executing. Whether modifying ENVIRON does anything to that environment is implementation-defined.
FILENAMEโ the pathname of the current input file. Inside aBEGINaction its value is undefined (don't ask, bestie ๐ญ). Inside anENDaction it's the name of the last input file processed.FNRโ the ordinal number of the current record in the current file. Inside aBEGINaction it's zero. Inside anENDaction it's the number of the last record processed in the last file processed.FSโ the input field separator, as a regular expression; defaults to a<space>. โจNFโ how many fields are in the current record. Inside aBEGINaction, usingNFis undefined unless agetlinefunction without a var argument was executed beforehand. Inside anENDaction,NFkeeps the value it had for the last record read โ unless a later, redirectedgetlinefunction without a var argument happens before you enter theENDaction.NRโ the ordinal number of the current record counted from the start of input. Inside aBEGINaction it's zero. Inside anENDaction it's the number of the last record processed.OFMTโ theprintfformat used to convert numbers into strings in output statements (see Output Statements);"%.6g"by default. If the value ofOFMTisn't a floating-point format specification, the result of the conversion is unspecified ๐.OFSโ the output field separator for theprintstatement;<space>by default.ORSโ the output record separator for theprintstatement; a<newline>by default.RLENGTHโ the length of the string matched by thematchfunction.RSโ the first character of the string value ofRSis the input record separator; a<newline>by default. IfRScontains more than one character, the results are unspecified ๐. IfRSis null, then records are separated by sequences consisting of a<newline>plus one or more blank lines; leading or trailing blank lines do not produce empty records at the beginning or end of the input, and a<newline>is always a field separator no matter whatFSis set to.RSTARTโ the starting position of the string matched by thematchfunction, counting from 1. It's always equivalent to the return value of thematchfunction. ๐SUBSEPโ the subscript separator string for multi-dimensional arrays; the default value is implementation-defined.
โจ Regular Expressions โจ
The awk utility uses the extended regular expression notation (see the Base Definitions volume of POSIX.1โ2017, Section 9.4, Extended Regular Expressions) โ with a lil bonus: it also allows C-language conventions for escaping special characters inside those EREs, exactly as specified in the table in the Base Definitions volume of POSIX.1โ2017, Chapter 5, File Format Notation ('\\', '\a', '\b', '\f', '\n', '\r', '\t', '\v') plus the table below. These escape sequences are recognized both inside and outside bracket expressions. Also, heads up: records don't have to be separated by <newline> characters, and string constants can totally contain <newline> characters, so even the "\n" sequence is valid in awk EREs. Using a <slash> character inside an ERE requires the escaping shown in this table:
Table 4-2: Escape Sequences in awk ๐
\"โ a<backslash>followed by a<quotation-mark>. Meaning: a<quotation-mark>character. Cute, right? ๐\/โ a<backslash>followed by a<slash>. Meaning: a<slash>character.\dddโ a<backslash>character followed by the longest sequence of one, two, or three octal-digit characters (01234567). If all of the digits are0(that is, a representation of the NUL character), the behavior is undefined ๐ญ. Meaning: the character whose encoding is represented by the one-, two-, or three-digit octal integer. Multibyte characters require multiple, concatenated escape sequences of this type, including the leading<backslash>for each byte.\cโ a<backslash>character followed by any character not described in this table or in the table in the Base Definitions volume of POSIX.1โ2017, Chapter 5, File Format Notation ('\\','\a','\b','\f','\n','\r','\t','\v'). Meaning: undefined. ๐
A regular expression can be matched against a specific field or string using one of the two regular expression matching operators, '~' and "!~". These operators interpret their right-hand operand as a regular expression and their left-hand operand as a string. If the regular expression matches the string, the '~' expression evaluates to a value of 1, and the "!~" expression evaluates to a value of 0. (The regular expression matching operation is as defined by the term matched in the Base Definitions volume of POSIX.1โ2017, Section 9.1, Regular Expression Definitions, where a match occurs on any part of the string unless the regular expression is limited with the <circumflex> or <dollar-sign> special characters.) If the regular expression does not match the string, the '~' expression evaluates to a value of 0, and the "!~" expression evaluates to a value of 1. If the right-hand operand is any expression other than the lexical token ERE, the string value of that expression is interpreted as an extended regular expression, including the escape conventions described above. Note that these same escape conventions are also applied when determining the value of a string literal (the lexical token STRING), and so they get applied a second time when a string literal is used in this context. ๐
When an ERE token shows up as an expression in any context other than as the right-hand side of the '~' or "!~" operator, or as one of the built-in function arguments described below, the value of the resulting expression is the equivalent of:
$0 ~ /ere/
The ere argument to the gsub, match, and sub functions, and the fs argument to the split function (see String Functions), are interpreted as extended regular expressions. These can be either ERE tokens or arbitrary expressions, and they're interpreted in the same manner as the right-hand side of the '~' or "!~" operator. ๐
An extended regular expression can be used to separate fields by assigning a string containing that expression to the built-in variable FS, either directly or as a consequence of using the -F sepstring option. The default value of the FS variable is a single <space>. Here's the tea on FS behavior โ:
- If
FSis a null string, the behavior is unspecified. - If
FSis a single character:- a. If
FSis<space>, skip leading and trailing<blank>and<newline>characters; fields are delimited by sets of one or more<blank>or<newline>characters. - b. Otherwise, if
FSis any other character c, fields are delimited by each single occurrence of c.
- a. If
- Otherwise, the string value of
FSis considered to be an extended regular expression. Each occurrence of a sequence matching the extended regular expression delimits fields. ๐ธ
Okay so here's the โจteaโจ on ERE matching: unless we're talking about the ~ and !~ operators, or you're hanging out inside the gsub, match, split, and sub built-in functions, ERE matching shall be based on input records. Translation bestie: the record separator characters โ that's the first character of the value of the variable RS, <newline> by default โ cannot be embedded in the expression, and no expression shall match the record separator character. Periodt. ๐
But plot twist ๐ซข if the record separator is not <newline>, then <newline> characters embedded in the expression can be matched. Slay.
For the ~ and !~ operators though, and in those four built-in functions, ERE matching shall be based on text strings. So literally any character (yes, including <newline> and the record separator) can be embedded in the pattern, and an appropriate pattern shall match any character. ๐
โ ๏ธ HOWEVER โ in all awk ERE matching, the use of one or more NUL characters in the pattern, input record, or text string produces undefined results. Not me finding that out the hard way ๐ญ
๐ Patterns ๐
A pattern is any valid expression, a range specified by two expressions separated by a comma, or one of the two special patterns BEGIN or END. That's the whole menu bestie ๐
โจ Special Patterns โจ
The awk utility shall recognize two special patterns: BEGIN and END. ๐
- Each
BEGINpattern shall be matched once and its associated action executed before the first record of input is read โ except possibly by use of thegetlinefunction (see Input/Output and General Functions) in a priorBEGINaction โ and before command line assignment is done. - Each
ENDpattern shall be matched once and its associated action executed after the last record of input has been read.
And yes, these two patterns shall have associated actions. No vibes-only BEGIN, sorry ๐ซถ
Rules of the house:
BEGINandENDshall not combine with other patterns.- Multiple
BEGINandENDpatterns shall be allowed. - The actions associated with the
BEGINpatterns shall be executed in the order specified in the program โ as are theENDactions. - An
ENDpattern can precede aBEGINpattern in a program. (Iconic, honestly.)
Now the exit behavior, because this one's a fave:
- If an awk program consists of only actions with the pattern
BEGIN, and theBEGINaction contains nogetlinefunction, awk shall exit without reading its input when the last statement in the lastBEGINaction is executed. - If an awk program consists of only actions with the pattern
END, or only actions with the patternsBEGINandEND, the input shall be read before the statements in theENDactions are executed.
๐ธ Expression Patterns ๐ธ
An expression pattern shall be evaluated as if it were an expression in a Boolean context.
- Result is true โ the pattern shall be considered to match, and the associated action (if any) shall be executed.
- Result is false โ the action shall not be executed.
Simple as that. Vibe check passed or failed, no in-between ๐
๐ฆ Pattern Ranges ๐ฆ
A pattern range consists of two expressions separated by a comma. In this case, the action shall be performed for all records between a match of the first expression and the following match of the second expression, inclusive.
And at this point, the pattern range can be repeated starting at input records subsequent to the end of the matched range. It's giving loop energy, but make it ranges ๐ซ
๐ Actions ๐
An action is a sequence of statements, exactly as shown in the grammar in Grammar. Any single statement can be replaced by a statement list enclosed in curly braces. The application shall ensure that statements in a statement list are separated by <newline> or <semicolon> characters. And statements in a statement list shall be executed sequentially, in the order that they appear. No skipping the queue ๐ซ
The expression acting as the conditional in an if statement shall be evaluated and:
- if it is non-zero or non-null, the following statement shall be executed;
- otherwise, if
elseis present, the statement following theelseshall be executed.
The if, while, do...while, for, break, and continue statements are based on the ISO C standard (see Section 1.1.2, Concepts Derived from the ISO C Standard), except that the Boolean expressions shall be treated as described in Expressions in awk, and except in the case of:
for (variable in array)
which shall iterate, assigning each index of array to variable in an unspecified order.
๐ The results of adding new elements toarraywithin such aforloop are undefined. And if abreakorcontinuestatement occurs outside of a loop, the behavior is undefined. Don't do it bestie, I'm begging ๐
The delete statement shall remove an individual array element. So to nuke an entire array, the following code does the job:
for (index in array)
delete array[index]
The next statement shall cause all further processing of the current input record to be abandoned. And heads up: the behavior is undefined if a next statement appears or is invoked in a BEGIN or END action. โ ๏ธ
The exit statement shall invoke all END actions in the order in which they occur in the program source and then terminate the program without reading further input. An exit statement inside an END action shall terminate the program without further execution of END actions. And if an expression is specified in an exit statement, its numeric value shall be the exit status of awk โ unless subsequent errors are encountered, or a subsequent exit statement with an expression is executed. ๐ฎโ๐จ
๐ Output Statements ๐
Both print and printf statements shall write to standard output by default. But if an output_redirection is supplied, the output shall be written to the location it specifies, as follows:
> expression
>> expression
| expression
In all cases, the expression shall be evaluated to produce a string that is used as a pathname into which to write (for > or >>) or as a command to be executed (for |). ๐ฏ
Using the first two forms:
- If the file of that name is not currently open, it shall be opened, creating it if necessary โ and using the first form, truncating the file.
- The output then shall be appended to the file.
- As long as the file remains open, subsequent calls in which
expressionevaluates to the same string value shall simply append output to the file. - The file remains open until the
closefunction (see Input/Output and General Functions) is called with an expression that evaluates to the same string value.
The third form shall write output onto a stream piped to the input of a command. ๐
- The stream shall be created if no stream is currently open with the value of
expressionas its command name. - The stream created shall be equivalent to one created by a call to the
popen()function defined in the System Interfaces volume of POSIX.1โ2017, with the value ofexpressionas the command argument and a value ofwas the mode argument. - As long as the stream remains open, subsequent calls in which
expressionevaluates to the same string value shall write output to the existing stream. - The stream shall remain open until the
closefunction (see Input/Output and General Functions) is called with an expression that evaluates to the same string value. At that time, the stream shall be closed as if by a call to thepclose()function defined in the System Interfaces volume of POSIX.1โ2017.
As described in detail by the grammar in Grammar, these output statements shall take a <comma>-separated list of expressions, referred to in the grammar by the non-terminal symbols expr_list, print_expr_list, or print_expr_list_opt. This list is referred to here as the expression list, and each member is referred to as an expression argument. ๐
๐ The print statement ๐
The print statement shall write the value of each expression argument onto the indicated output stream:
- separated by the current output field separator (see variable
OFSabove), and - terminated by the output record separator (see variable
ORSabove).
All expression arguments shall be taken as strings, being converted if necessary. This conversion shall be as described in Expressions in awk, with the exception that the printf format in OFMT shall be used instead of the value in CONVFMT. And an empty expression list shall stand for the whole input record ($0). ๐
โญ The printf statement โญ
The printf statement shall produce output based on a notation similar to the File Format Notation used to describe file formats in this volume of POSIX.1โ2017 (see the Base Definitions volume of POSIX.1โ2017, Chapter 5, File Format Notation). Output shall be produced as specified with the first expression argument as the string format and subsequent expression arguments as the strings arg1 to argn, inclusive โ with the following exceptions:
- The format shall be an actual character string rather than a graphical representation. Therefore, it cannot contain empty character positions. The
<space>in the format string, in any context other than a flag of a conversion specification, shall be treated as an ordinary character that is copied to the output. - If the character set contains a
''character and that character appears in the format string, it shall be treated as an ordinary character that is copied to the output. - The escape sequences beginning with a
<backslash>character shall be treated as sequences of ordinary characters that are copied to the output. Note that these same sequences shall be interpreted lexically by awk when they appear in literal strings, but they shall not be treated specially by theprintfstatement. - A field width or precision can be specified as the
*character instead of a digit string. In this case the next argument from the expression list shall be fetched and its numeric value taken as the field width or precision. - The implementation shall not precede or follow output from the
doruconversion specifier characters with<blank>characters not specified by the format string. - The implementation shall not precede output from the
oconversion specifier character with leading zeros not specified by the format string. - For the
cconversion specifier character: if the argument has a numeric value, the character whose encoding is that value shall be output. If the value is zero or is not the encoding of any character in the character set, the behavior is undefined. If the argument does not have a numeric value, the first character of the string value shall be output; if the string does not contain any characters, the behavior is undefined. - For each conversion specification that consumes an argument, the next expression argument shall be evaluated. With the exception of the
cconversion specifier character, the value shall be converted (according to the rules specified in Expressions in awk) to the appropriate type for the conversion specification. - If there are insufficient expression arguments to satisfy all the conversion specifications in the format string, the behavior is undefined.
- If any character sequence in the format string begins with a
%character, but does not form a valid conversion specification, the behavior is unspecified.
Both print and printf can output at least {LINE_MAX} bytes. ๐
โจ Functions โจ
okay so awk comes STOCKED with built-in functions, bestie ๐ and they come in four flavours: arithmetic, string, input/output, and general. iconic lineup fr fr.
๐งฎ Arithmetic Functions
The arithmetic functions, except for int, shall be based on the ISO C standard (see Section 1.1.2, Concepts Derived from the ISO C Standard). The behavior is undefined in cases where the ISO C standard specifies that an error be returned or that the behavior is undefined.
๐ซถ heads up bestie: although the grammar (see Grammar) permits built-in functions to appear with no arguments or parentheses, unless the argument or parentheses are indicated as optional in the following list (by displaying them within the "[]" brackets), such use is undefined. so don't do that.
atan2(y,x)โ Return arctangent ofy/xin radians in the range[-ฯ,ฯ]. ๐cos(x)โ Return cosine ofx, wherexis in radians.sin(x)โ Return sine ofx, wherexis in radians.exp(x)โ Return the exponential function ofx. โจlog(x)โ Return the natural logarithm ofx.sqrt(x)โ Return the square root ofx.int(x)โ Return the argument truncated to an integer. Truncation shall be toward0whenx>0.rand()โ Return a random numbern, such that0โคn<1. ๐ฒsrand([expr])โ Set the seed value forrandtoexpror use the time of day ifexpris omitted. The previous seed value shall be returned.
๐ String Functions
The string functions in the following list shall be supported. And again, same little disclaimer as before: although the grammar (see Grammar) permits built-in functions to appear with no arguments or parentheses, unless the argument or parentheses are indicated as optional in the following list (by displaying them within the "[]" brackets), such use is undefined. no cap.
gsub(ere, repl[, in])โ Behave likesub(see below), except that it shall replace all occurrences of the regular expression (like theedutility global substitute) in$0or in theinargument, when specified. ๐index(s, t)โ Return the position, in characters, numbering from1, in stringswhere stringtfirst occurs, or zero if it does not occur at all.length[([s])]โ Return the length, in characters, of its argument taken as a string, or of the whole record,$0, if there is no argument.match(s, ere)โ Return the position, in characters, numbering from1, in stringswhere the extended regular expressionereoccurs, or zero if it does not occur at all.RSTARTshall be set to the starting position (which is the same as the returned value), zero if no match is found;RLENGTHshall be set to the length of the matched string,-1if no match is found. ๐split(s, a[, fs ])โ Split the stringsinto array elementsa[1],a[2], ...,a[n], and returnn. All elements of the array shall be deleted before the split is performed. The separation shall be done with the EREfsor with the field separatorFSiffsis not given. Each array element shall have a string value when created and, if appropriate, the array element shall be considered a numeric string (see Expressions in awk). The effect of a null string as the value offsis unspecified.sprintf(fmt, expr, expr, ...)โ Format the expressions according to theprintfformat given byfmtand return the resulting string. โจsub(ere, repl[, in ])โ Substitute the stringreplin place of the first instance of the extended regular expression ERE in stringinand return the number of substitutions. An<ampersand>('&') appearing in the stringreplshall be replaced by the string frominthat matches the ERE. An<ampersand>preceded with a<backslash>shall be interpreted as the literal<ampersand>character. An occurrence of two consecutive<backslash>characters shall be interpreted as just a single literal<backslash>character. Any other occurrence of a<backslash>(for example, preceding any other character) shall be treated as a literal<backslash>character. Note that ifreplis a string literal (the lexical token STRING; see Grammar), the handling of the<ampersand>character occurs after any lexical processing, including any lexical<backslash>-escape sequence processing. Ifinis specified and it is not an lvalue (see Expressions in awk), the behavior is undefined. Ifinis omitted, awk shall use the current record ($0) in its place.substr(s, m[, n ])โ Return the at mostn-character substring ofsthat begins at positionm, numbering from1. Ifnis omitted, or ifnspecifies more characters than are left in the string, the length of the substring shall be limited by the length of the strings.tolower(s)โ Return a string based on the strings. Each character insthat is an uppercase letter specified to have atolowermapping by theLC_CTYPEcategory of the current locale shall be replaced in the returned string by the lowercase letter specified by the mapping. Other characters insshall be unchanged in the returned string. ๐ธtoupper(s)โ Return a string based on the strings. Each character insthat is a lowercase letter specified to have atouppermapping by theLC_CTYPEcategory of the current locale is replaced in the returned string by the uppercase letter specified by the mapping. Other characters insare unchanged in the returned string.
All of the preceding functions that take ERE as a parameter expect a pattern or a string valued expression that is a regular expression as defined in Regular Expressions. ๐
๐ฆ Input/Output and General Functions
The input/output and general functions are:
close(expression)โ Close the file or pipe opened by aprintorprintfstatement or a call togetlinewith the same string-valuedexpression. The limit on the number of openexpressionarguments is implementation-defined. If the close was successful, the function shall return zero; otherwise, it shall return non-zero.expression | getline [var]โ Read a record of input from a stream piped from the output of a command. The stream shall be created if no stream is currently open with the value ofexpressionas its command name. The stream created shall be equivalent to one created by a call to thepopen()function with the value ofexpressionas the command argument and a value ofras the mode argument. As long as the stream remains open, subsequent calls in whichexpressionevaluates to the same string value shall read subsequent records from the stream. The stream shall remain open until theclosefunction is called with anexpressionthat evaluates to the same string value. At that time, the stream shall be closed as if by a call to thepclose()function. Ifvaris omitted,$0andNFshall be set; otherwise,varshall be set and, if appropriate, it shall be considered a numeric string (see Expressions in awk).
The getline operator can form ambiguous constructs when there are unparenthesized operators (including concatenate) to the left of the '|' (to the beginning of the expression containing getline). In the context of the '$' operator, '|' shall behave as if it had a lower precedence than '$'. The result of evaluating other operators is unspecified, and conforming applications shall parenthesize properly all such usages. ๐
getlineโ Set$0to the next input record from the current input file. This form ofgetlineshall set theNF,NR, andFNRvariables.getline varโ Set variablevarto the next input record from the current input file and, if appropriate,varshall be considered a numeric string (see Expressions in awk). This form ofgetlineshall set theFNRandNRvariables.getline [var] < expressionโ Read the next record of input from a named file. Theexpressionshall be evaluated to produce a string that is used as a pathname. If the file of that name is not currently open, it shall be opened. As long as the stream remains open, subsequent calls in whichexpressionevaluates to the same string value shall read subsequent records from the file. The file shall remain open until theclosefunction is called with anexpressionthat evaluates to the same string value. Ifvaris omitted,$0andNFshall be set; otherwise,varshall be set and, if appropriate, it shall be considered a numeric string (see Expressions in awk).
The getline operator can form ambiguous constructs when there are unparenthesized binary operators (including concatenate) to the right of the '<' (up to the end of the expression containing the getline). The result of evaluating such a construct is unspecified, and conforming applications shall parenthesize properly all such usages. ๐ญ
system(expression)โ Execute the command given byexpressionin a manner equivalent to thesystem()function defined in the System Interfaces volume of POSIX.1โ2017 and return the exit status of the command.
All forms of getline shall return 1 for successful input, zero for end-of-file, and -1 for an error. โญ
Where strings are used as the name of a file or pipeline, the application shall ensure that the strings are textually identical. The terminology same string value'' implies that equivalent strings'', even those that differ only by <space> characters, represent different files. ๐
๐ง User-Defined Functions
The awk language also provides user-defined functions, bestie โ yes you can write your OWN. Such functions can be defined as:
function name([parameter, ...]) { statements }
A function can be referred to anywhere in an awk program; in particular, its use can precede its definition. The scope of a function is global. ๐
Function parameters, if you've got any, can be either scalars or arrays โ but bestie, the behavior is undefined if you pass an array name as a parameter that the function then uses as a scalar, or if you pass a scalar expression as a parameter that the function uses as an array. So likeโฆ don't do that. ๐ซ
The tea on passing: function parameters shall be passed by value if scalar and by reference if array name. That's the whole vibe.
The number of parameters in the function definition does <em>not</em> have to match the number in the function call, and honestly that's kind of iconic of awk. Excess formal parameters can be used as local variables (a little hack, a little slay ๐ ). If you supply <em>fewer</em> arguments in the call than the definition has, then:
- extra parameters used in the body as <strong>scalars</strong> shall evaluate to the uninitialized value until they get initialized otherwise โจ
- extra parameters used in the body as <strong>arrays</strong> shall be treated as uninitialized arrays, where each element evaluates to the uninitialized value until otherwise initialized ๐ฆ
When you invoke a function, <strong>no white space</strong> can be placed between the function name and the opening parenthesis. Not even one space. Not even a sneaky little tab. ๐ซ
Function calls can be nested, and recursive calls can be made upon functions. Upon return from any nested or recursive function call, the values of all of the calling function's parameters shall be unchanged โ <em>except</em> for array parameters passed by reference (those ones are shared, bestie, that's the whole point ๐).
The return statement can be used to return a value. And if a return statement appears <strong>outside</strong> of a function definition? Behavior is undefined. Not me finding out the hard way ๐ญ
In the function definition, <newline> characters shall be optional before the opening brace and after the closing brace. Function definitions can appear anywhere in the program where a pattern-action pair is allowed. Anywhere! Main character energy ๐
โจ Grammar โจ
The grammar in this section and the lexical conventions in the following section shall <strong>together</strong> describe the syntax for awk programs. The general conventions for this style of grammar are described in Section 1.3, <em>Grammar Conventions</em>. A valid program can be represented as the non-terminal symbol <em>program</em> in the grammar. And this formal syntax <strong>shall take precedence</strong> over the preceding text syntax description โ so if the prose and the grammar ever beef, the grammar wins. Periodt. ๐
%token NAME NUMBER STRING ERE
%token FUNC_NAME /* Name followed by '(' without white space. */
/* Keywords */
%token Begin End
/* 'BEGIN' 'END' */
%token Break Continue Delete Do Else
/* 'break' 'continue' 'delete' 'do' 'else' */
%token Exit For Function If In
/* 'exit' 'for' 'function' 'if' 'in' */
%token Next Print Printf Return While
/* 'next' 'print' 'printf' 'return' 'while' */
/* Reserved function names */
%token BUILTIN_FUNC_NAME
/* One token for the following:
* atan2 cos sin exp log sqrt int rand srand
* gsub index length match split sprintf sub
* substr tolower toupper close system
*/
%token GETLINE
/* Syntactically different from other built-ins. */
/* Two-character tokens. */
%token ADD_ASSIGN SUB_ASSIGN MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN POW_ASSIGN
/* '+=' '-=' '*=' '/=' '%=' '^=' */
%token OR AND NO_MATCH EQ LE GE NE INCR DECR APPEND
/* '||' '&&' '!~' '==' '<=' '>=' '!=' '++' '--' '>>' */
/* One-character tokens. */
%token '{' '}' '(' ')' '[' ']' ',' ';' NEWLINE
%token '+' '-' '*' '%' '^' '!' '>' '<' '|' '?' ':' '~' '$' '='
%start program
%%
program : item_list
| item_list item
;
item_list : /* empty */
| item_list item terminator
;
item : action
| pattern action
| normal_pattern
| Function NAME '(' param_list_opt ')'
newline_opt action
| Function FUNC_NAME '(' param_list_opt ')'
newline_opt action
;
param_list_opt : /* empty */
| param_list
;
param_list : NAME
| param_list ',' NAME
;
pattern : normal_pattern
| special_pattern
;
normal_pattern : expr
| expr ',' newline_opt expr
;
special_pattern : Begin
| End
;
action : '{' newline_opt '}'
| '{' newline_opt terminated_statement_list '}'
| '{' newline_opt unterminated_statement_list '}'
;
terminator : terminator NEWLINE
| ';'
| NEWLINE
;
terminated_statement_list : terminated_statement
| terminated_statement_list terminated_statement
;
unterminated_statement_list : unterminated_statement
| terminated_statement_list unterminated_statement
;
terminated_statement : action newline_opt
| If '(' expr ')' newline_opt terminated_statement
| If '(' expr ')' newline_opt terminated_statement
Else newline_opt terminated_statement
| While '(' expr ')' newline_opt terminated_statement
| For '(' simple_statement_opt ';'
expr_opt ';' simple_statement_opt ')' newline_opt
terminated_statement
| For '(' NAME In NAME ')' newline_opt
terminated_statement
| ';' newline_opt
| terminatable_statement NEWLINE newline_opt
| terminatable_statement ';' newline_opt
;
unterminated_statement : terminatable_statement
| If '(' expr ')' newline_opt unterminated_statement
| If '(' expr ')' newline_opt terminated_statement
Else newline_opt unterminated_statement
| While '(' expr ')' newline_opt unterminated_statement
| For '(' simple_statement_opt ';'
expr_opt ';' simple_statement_opt ')' newline_opt
unterminated_statement
| For '(' NAME In NAME ')' newline_opt
unterminated_statement
;
terminatable_statement : simple_statement
| Break
| Continue
| Next
| Exit expr_opt
| Return expr_opt
| Do newline_opt terminated_statement While '(' expr ')'
;
simple_statement_opt : /* empty */
| simple_statement
;
simple_statement : Delete NAME '[' expr_list ']'
| expr
| print_statement
;
print_statement : simple_print_statement
| simple_print_statement output_redirection
;
simple_print_statement : Print print_expr_list_opt
| Print '(' multiple_expr_list ')'
| Printf print_expr_list
| Printf '(' multiple_expr_list ')'
;
output_redirection : '>' expr
| APPEND expr
| '|' expr
;
expr_list_opt : /* empty */
| expr_list
;
expr_list : expr
| multiple_expr_list
;
multiple_expr_list : expr ',' newline_opt expr
| multiple_expr_list ',' newline_opt expr
;
expr_opt : /* empty */
| expr
;
expr : unary_expr
| non_unary_expr
;
unary_expr : '+' expr
| '-' expr
| unary_expr '^' expr
| unary_expr '*' expr
| unary_expr '/' expr
| unary_expr '%' expr
| unary_expr '+' expr
| unary_expr '-' expr
| unary_expr non_unary_expr
| unary_expr '<' expr
| unary_expr LE expr
| unary_expr NE expr
| unary_expr EQ expr
| unary_expr '>' expr
| unary_expr GE expr
| unary_expr '~' expr
| unary_expr NO_MATCH expr
| unary_expr In NAME
| unary_expr AND newline_opt expr
| unary_expr OR newline_opt expr
| unary_expr '?' expr ':' expr
| unary_input_function
;
๐ No cap, that's a <em>lot</em> of grammar, and we're not even done yet โ keep scrolling, bestie.
โฆand the grammar just keeps going, bestie, it's giving main character energy:
non_unary_expr : '(' expr ')'
| '!' expr
| non_unary_expr '^' expr
| non_unary_expr '*' expr
| non_unary_expr '/' expr
| non_unary_expr '%' expr
| non_unary_expr '+' expr
| non_unary_expr '-' expr
| non_unary_expr non_unary_expr
| non_unary_expr '<' expr
| non_unary_expr LE expr
| non_unary_expr NE expr
| non_unary_expr EQ expr
| non_unary_expr '>' expr
| non_unary_expr GE expr
| non_unary_expr '~' expr
| non_unary_expr NO_MATCH expr
| non_unary_expr In NAME
| '(' multiple_expr_list ')' In NAME
| non_unary_expr AND newline_opt expr
| non_unary_expr OR newline_opt expr
| non_unary_expr '?' expr ':' expr
| NUMBER
| STRING
| lvalue
| ERE
| lvalue INCR
| lvalue DECR
| INCR lvalue
| DECR lvalue
| lvalue POW_ASSIGN expr
| lvalue MOD_ASSIGN expr
| lvalue MUL_ASSIGN expr
| lvalue DIV_ASSIGN expr
| lvalue ADD_ASSIGN expr
| lvalue SUB_ASSIGN expr
| lvalue '=' expr
| FUNC_NAME '(' expr_list_opt ')'
/* no white space allowed before '(' */
| BUILTIN_FUNC_NAME '(' expr_list_opt ')'
| BUILTIN_FUNC_NAME
| non_unary_input_function
;
print_expr_list_opt : /* empty */
| print_expr_list
;
print_expr_list : print_expr
| print_expr_list ',' newline_opt print_expr
;
print_expr : unary_print_expr
| non_unary_print_expr
;
unary_print_expr : '+' print_expr
| '-' print_expr
| unary_print_expr '^' print_expr
| unary_print_expr '*' print_expr
| unary_print_expr '/' print_expr
| unary_print_expr '%' print_expr
| unary_print_expr '+' print_expr
| unary_print_expr '-' print_expr
| unary_print_expr non_unary_print_expr
| unary_print_expr '~' print_expr
| unary_print_expr NO_MATCH print_expr
| unary_print_expr In NAME
| unary_print_expr AND newline_opt print_expr
| unary_print_expr OR newline_opt print_expr
| unary_print_expr '?' print_expr ':' print_expr
;
non_unary_print_expr : '(' expr ')'
| '!' print_expr
| non_unary_print_expr '^' print_expr
| non_unary_print_expr '*' print_expr
| non_unary_print_expr '/' print_expr
| non_unary_print_expr '%' print_expr
| non_unary_print_expr '+' print_expr
| non_unary_print_expr '-' print_expr
| non_unary_print_expr non_unary_print_expr
| non_unary_print_expr '~' print_expr
| non_unary_print_expr NO_MATCH print_expr
| non_unary_print_expr In NAME
| '(' multiple_expr_list ')' In NAME
| non_unary_print_expr AND newline_opt print_expr
| non_unary_print_expr OR newline_opt print_expr
| non_unary_print_expr '?' print_expr ':' print_expr
| NUMBER
| STRING
| lvalue
| ERE
| lvalue INCR
| lvalue DECR
| INCR lvalue
| DECR lvalue
| lvalue POW_ASSIGN print_expr
| lvalue MOD_ASSIGN print_expr
| lvalue MUL_ASSIGN print_expr
| lvalue DIV_ASSIGN print_expr
| lvalue ADD_ASSIGN print_expr
| lvalue SUB_ASSIGN print_expr
| lvalue '=' print_expr
| FUNC_NAME '(' expr_list_opt ')'
/* no white space allowed before '(' */
| BUILTIN_FUNC_NAME '(' expr_list_opt ')'
| BUILTIN_FUNC_NAME
;
lvalue : NAME
| NAME '[' expr_list ']'
| '$' expr
;
non_unary_input_function : simple_get
| simple_get '<' expr
| non_unary_expr '|' simple_get
;
unary_input_function : unary_expr '|' simple_get
;
simple_get : GETLINE
| GETLINE lvalue
;
newline_opt : /* empty */
| newline_opt NEWLINE
;
This grammar has several ambiguities that shall be resolved as follows:
- Operator precedence and associativity shall be as described in Table 4-1, Expressions in Decreasing Precedence in
awk. - In case of ambiguity, an
elseshall be associated with the most immediately precedingifthat would satisfy the grammar. - In some contexts, a
<slash>('/') that is used to surround an ERE could also be the division operator. This shall be resolved in such a way that wherever the division operator could appear, a<slash>is assumed to be the division operator. (There is no unary division operator.)
Each expression in an awk program shall conform to the precedence and associativity rules, even when this is not needed to resolve an ambiguity. For example, because '$' has higher precedence than '++', the string "$x++--" is not a valid awk expression, even though it is unambiguously parsed by the grammar as "$(x++)--". ๐ญ like it literally looks fine and it is NOT fine.
One convention that might not be obvious from the formal grammar is where <newline> characters are acceptable. There are several obvious placements such as terminating a statement, and a <backslash> can be used to escape <newline> characters between any lexical tokens. In addition, <newline> characters without <backslash> characters can follow a comma, an open brace, logical AND operator ("&&"), logical OR operator ("||"), the do keyword, the else keyword, and the closing parenthesis of an if, for, or while statement. For example:
{ print $1,
$2 }
๐ฆ Lexical Conventions ๐ฆ
The lexical conventions for awk programs, with respect to the preceding grammar, shall be as follows:
- Except as noted,
awkshall recognize the longest possible token or delimiter beginning at a given point. (greedy queen behavior ๐ ) - A comment shall consist of any characters beginning with the
<number-sign>character and terminated by, but excluding the next occurrence of, a<newline>. Comments shall have no effect, except to delimit lexical tokens. - The
<newline>shall be recognized as the token NEWLINE. - A
<backslash>character immediately followed by a<newline>shall have no effect. - The token STRING shall represent a string constant. A string constant shall begin with the character
'"'. Within a string constant, a<backslash>character shall be considered to begin an escape sequence as specified in the table in the Base Definitions volume of POSIX.1โ2017, Chapter 5, File Format Notation ('\\','\a','\b','\f','\n','\r','\t','\v'). In addition, the escape sequences in Table 4-2, Escape Sequences inawkshall be recognized. A<newline>shall not occur within a string constant. A string constant shall be terminated by the first unescaped occurrence of the character'"'after the one that begins the string constant. The value of the string shall be the sequence of all unescaped characters and values of escape sequences between, but not including, the two delimiting'"'characters. โจ - The token ERE represents an extended regular expression constant. An ERE constant shall begin with the
<slash>character. Within an ERE constant, a<backslash>character shall be considered to begin an escape sequence as specified in the table in the Base Definitions volume of POSIX.1โ2017, Chapter 5, File Format Notation. In addition, the escape sequences in Table 4-2, Escape Sequences inawkshall be recognized. The application shall ensure that a<newline>does not occur within an ERE constant. An ERE constant shall be terminated by the first unescaped occurrence of the<slash>character after the one that begins the ERE constant. The extended regular expression represented by the ERE constant shall be the sequence of all unescaped characters and values of escape sequences between, but not including, the two delimiting<slash>characters. ๐ - A
<blank>shall have no effect, except to delimit lexical tokens or within STRING or ERE tokens. - The token NUMBER shall represent a numeric constant. Its form and numeric value shall either be equivalent to the decimal-floating-constant token as specified by the ISO C standard, or it shall be a sequence of decimal digits and shall be evaluated as an integer constant in decimal. In addition, implementations may accept numeric constants with the form and numeric value equivalent to the hexadecimal-constant and hexadecimal-floating-constant tokens as specified by the ISO C standard.
If the value is too large or too small to be representable (see Section 1.1.2, Concepts Derived from the ISO C Standard), the behavior is undefined. ๐ no cap, that's a "we don't know her" situation and you do NOT want to meet her.
- So, a word โ bestie, this one's simple โ is a sequence of underscores, digits, and alphabetics, all from the portable character set (peep the Base Definitions volume of POSIX.1โ2017, Section 6.1, Portable Character Set). But it has to start with an
<underscore>or an alphabetic character. No exceptions. Periodt. ๐ - These words are keywords, and they get recognized as individual tokens โ the name of the token is literally the same as the keyword itself:
BEGIN delete END function in printf
break do exit getline next return
continue else for if print while
- And these are the names of built-in functions โ they get recognized as the token
BUILTIN_FUNC_NAME:
atan2 gsub log split sub toupper
close index match sprintf substr
cos int rand sqrt system
exp length sin srand tolower
Heads up: all the keywords up there โ๏ธ and all the built-in function names up there โ๏ธ are considered reserved words. You can't just claim them for yourself, bestie.
- The token
NAMEis a word that is not a keyword, is not a built-in function name, and is not followed immediately (like, with zero delimiters in between) by the'('character. - The token
FUNC_NAMEis a word that is not a keyword, is not a built-in function name, and is followed immediately (again, no delimiters, none) by the'('character. And that'('? It does not get included as part of the token. It's just the vibe check at the door. ๐ช - These two-character sequences get recognized as named tokens, and here's the whole lineup:
โโโโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโโโฌโโโโโโโโโโโ
โ Token Name โ Sequence โ Token Name โ Sequence โ
โโโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโโโผโโโโโโโโโโโค
โ ADD_ASSIGN โ += โ NO_MATCH โ !~ โ
โ SUB_ASSIGN โ -= โ EQ โ == โ
โ MUL_ASSIGN โ *= โ LE โ <= โ
โ DIV_ASSIGN โ /= โ GE โ >= โ
โ MOD_ASSIGN โ %= โ NE โ != โ
โ POW_ASSIGN โ ^= โ INCR โ ++ โ
โ OR โ || โ DECR โ -- โ
โ AND โ && โ APPEND โ >> โ
โโโโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโโโโ
- And these single characters get recognized as tokens whose names are literally the character itself:
<newline> { } ( ) [ ] , ; + - * % ^ ! > < | ? : ~ $ =
okay okay okay, here comes the one bit of DRAMA in the lexer โจ
There is a lexical ambiguity between the token ERE and the tokens '/' and DIV_ASSIGN. Let's break it down:
- If an input sequence starts with a
<slash>character, and you're in any syntactic context where the token'/'orDIV_ASSIGNcould show up as the next token in a valid program, then whichever of those two tokens is longer and can be recognized โ that's the one that gets recognized. - In any other syntactic context where the token
EREcould appear as the next token in a valid program, the tokenEREgets recognized instead.
Slash really said "I contain multitudes" ๐
โจ EXIT STATUS โจ
Here's what you get back when the whole thing wraps up:
0โ all input files were processed successfully. Slay. ๐>0โ an error occurred. not slay. ๐ญ
And ngl, you can change that exit status from inside your program โ just use an exit expression. Main character move fr fr.
๐ CONSEQUENCES OF ERRORS ๐
Okay so if you name a file operand and that file cannot be accessed, awk writes a diagnostic message to standard error and then terminates immediately โ no further action, no second chances, it's giving ghosted. ๐ป
And if the program you passed in via either the program operand or a progfile operand is not a valid awk program (as specified over in the EXTENDED DESCRIPTION section)? The behavior is undefined. We don't know what happens. Nobody knows. Chaos. ๐
The following sections are informative.
๐ซถ APPLICATION USAGE ๐ซถ
First things first โ do not get the index, length, match, and substr functions confused with the similarly-named functions in the ISO C standard. They are not the same girlies. The awk versions deal with characters, while the ISO C standard deals with bytes. Different lanes, different energy. ๐
Because concatenation is represented by just... putting expressions next to each other (no explicit operator, no little symbol, they just hold hands ๐ซฑ๐ซฒ), you'll often need parentheses to enforce the proper evaluation precedence. Don't skip the parentheses, bestie, they're load-bearing.
When you're using awk to process pathnames, it is recommended that LC_ALL, or at least LC_CTYPE and LC_COLLATE, are set to POSIX or C in the environment. Why? Because pathnames can contain byte sequences that don't form valid characters in some locales, and in that case the utility's behavior would be undefined. In the POSIX locale, every byte is a valid single-byte character, so the whole problem just... doesn't exist. Avoided. Iconic.
Now, on implementations where the "==" operator checks if strings collate equally, and your app needs to check whether strings are identical, you can use:
length(a) == length(b) && index(a,b) == 1
And flip side โ on implementations where the "==" operator checks if strings are identical, and your app needs to check whether strings collate equally, you can use:
a <= b && a >= b
Two-way street, both directions covered, we love a flexible queen. ๐
๐ธ EXAMPLES ๐ธ
The awk program you put on the command line is easiest to specify inside single-quotes (like 'program') for applications using sh, because awk programs are constantly full of characters that the shell considers special โ double-quotes included, yes ma'am.
But what if your awk program contains single-quote characters? ๐ณ Usually the easiest move is to specify most of the program as strings within single-quotes, concatenated by the shell with quoted single-quote characters. Like this:
awk '/'\''/ { print "quote:", $0 }'
That one prints all lines from standard input containing a single-quote character, each prefixed with quote:. Cute, right? ๐
And now the moment you've been waiting for โ the simple awk program showcase ๐
- Write to standard output all input lines for which field 3 is greater than 5:
$3 > 5
- Write every tenth line:
(NR % 10) == 0
- Write any line with a substring matching the regular expression:
/(G|D)(2[0-9][[:alpha:]]*)/
- Print any line with a substring containing a
'G'or'D', followed by a sequence of digits and characters. This example uses the character classesdigitandalphato match language-independent digit and alphabetic characters respectively:
/(G|D)([[:digit:][:alpha:]]*)/
- Write any line in which the second field matches the regular expression and the fourth field does not:
$2 ~ /xyz/ && $4 !~ /xyz/
- Write any line in which the second field contains a
<backslash>:
$2 ~ /\\/
- Write any line in which the second field contains a
<backslash>. Note that<backslash>-escapes are interpreted twice; once in lexical processing of the string and once in processing the regular expression:
$2 ~ "\\\\"
Yes bestie, that's four backslashes. Twice the escaping, twice the chaos. This is not a typo. ๐
- Write the second to the last and the last field in each line. Separate the fields by a
<colon>:
{OFS=":";print $(NF-1), $NF}
- Write the line number and number of fields in each line. The three strings representing the line number, the
<colon>, and the number of fields are concatenated, and that string is written to standard output:
{print NR ":" NF}
- Write lines longer than 72 characters:
length($0) > 72
- Write the first two fields in opposite order, separated by
OFS:
{ print $2, $1 }
- Same thing, but with input fields separated by a
<comma>or a<space>and<tab>characters, or both:
BEGIN { FS = ",[ \t]*|[ \t]+" }
{ print $2, $1 }
- Add up the first column, print the sum, and print the average:
{s += $1 }
END {print "sum is ", s, " average is", s/NR}
- Write fields in reverse order, one per line (many lines out for each line in):
{ for (i = NF; i > 0; --i) print $i }
- Write all lines between occurrences of the strings
startandstop:
/start/, /stop/
- Write all lines whose first field is different from the previous one:
$1 != prev { print; prev = $1 }
- Simulate
echo:
BEGIN {
for (i = 1; i < ARGC; ++i)
printf("%s%s", ARGV[i], i==ARGC-1?"\n":" ")
}
- Write the path prefixes contained in the
PATHenvironment variable, one per line:
BEGIN {
n = split (ENVIRON["PATH"], path, ":")
for (i = 1; i <= n; ++i)
print path[i]
}
- If there's a file named
inputcontaining page headers of the form:
Page #
and a file named program that contains:
/Page/ { $2 = n++; }
{ print }
then this command line:
awk -f program n=5 input
prints the file input, filling in page numbers starting at 5. numbering goes brrr โจ
๐ง RATIONALE ๐ง
This whole description is based on the new awk, nawk (see the referenced The AWK Programming Language), which introduced a bunch of new features to the historical awk. Let's count them down:
- New keywords:
delete,do,function,return - New built-in functions:
atan2,close,cos,gsub,match,rand,sin,srand,sub,system - New predefined variables:
FNR,ARGC,ARGV,RSTART,RLENGTH,SUBSEP - New expression operators:
?,:,,,^ - The
FSvariable and the third argument tosplit, now treated as extended regular expressions. - The operator precedence, changed to more closely match the C language. Two examples of code that operate differently are:
while ( n /= 10 > 1) ...
if (!"wk" ~ /bwk/) ...
And a whole handful of features got added based on newer implementations of awk:
- Multiple instances of
-f progfileare permitted. ๐ - The new option
-v assignment. - The new predefined variable
ENVIRON. - New built-in functions
toupperandtolower. - More formatting capabilities are added to
printfto match the ISO C standard.
and finally, a lil history lesson ๐
Earlier versions of this standard required implementations to support:
- multiple adjacent
<semicolon>s, - lines with one or more
<semicolon>before a rule (pattern-action pairs), and - lines with only
<semicolon>(s).
These are not required by this standard and are considered poor programming practice โ but an implementation of awk can accept them as an extension if it wants to. Do with that information what you will, bestie. I'd just... not. ๐
okay so awk's whole syntax? literally always been C-coded from day one, with a few lil features borrowed from the shell command language and other sources. ๐ซถ Which means it is not completely compatible with any other language โ and bestie, that has caused confusion for some users. It is not the intent of the standard developers to address such issues, so don't @ them. ๐
A few relatively minor changes toward making the language more compatible with the ISO C standard were made; most of these changes are based on similar changes in recent implementations, as described above. But there remain several C-language conventions that are not in awk. One of the notable ones is the <comma> operator, which in C is commonly used to specify multiple expressions in the for statement. Also, there are various places where awk is more restrictive than the C language regarding the type of expression that can be used in a given context. Those limitations are lowkey just because of the different features the awk language does provide. ๐คท
โจ regular expressions, but make it extra โจ
Regular expressions in awk have been extended somewhat from historical implementations to make them a pure superset of extended regular expressions, as defined by POSIX.1โ2008 (see the Base Definitions volume of POSIX.1โ2017, Section 9.4, Extended Regular Expressions). The main extensions are internationalization features and interval expressions. Historical implementations of awk have long supported <backslash>-escape sequences as an extension to extended regular expressions, and this extension has been retained despite inconsistency with other utilities. ๐ญ
The number of escape sequences recognized in both extended regular expressions and strings has varied (generally increasing with time) among implementations. The set specified by POSIX.1โ2008 includes most sequences known to be supported by popular implementations and by the ISO C standard. One sequence that is not supported is hexadecimal value escapes beginning with '\x'. Why? Because it would allow values expressed in more than 9 bits to be used within awk, like in the ISO C standard โ but because this syntax has a non-deterministic length, it does not permit the subsequent character to be a hexadecimal digit. In the C language this limitation can be dealt with by lexical string concatenation. In awk, concatenation could also be a solution for strings, but not for extended regular expressions (either lexical ERE tokens or strings used dynamically as regular expressions). Because of this limitation, the feature has not been added to POSIX.1โ2008. ๐ซ
When a string variable is used in a context where an extended regular expression normally appears (where the lexical token ERE is used in the grammar), the string does not contain the literal <slash> characters. Periodt.
Some versions of awk allow the form:
func name(args, ... ) { statements }
This has been deprecated by the authors of the language, who asked that it not be specified. ๐
Historical implementations of awk produce an error if a next statement is executed in a BEGIN action, and cause awk to terminate if a next statement is executed in an END action. This behavior has not been documented, and it was not believed that it was necessary to standardize it.
๐ strings vs. numbers, the saga ๐
The specification of conversions between string and numeric values is much more detailed than in the documentation of historical implementations or in the referenced The AWK Programming Language. Most of the behavior is designed to be intuitive, but the details are necessary to ensure compatible behavior from different implementations. This is especially important in relational expressions, since the types of the operands determine whether a string or numeric comparison is performed. ๐ซ
From the perspective of an application developer, it is usually sufficient to expect intuitive behavior and to force conversions (by adding zero or concatenating a null string) when the type of an expression does not obviously match what is needed. The intent has been to specify historical practice in almost all cases. The one exception? In historical implementations, variables and constants maintain both string and numeric values after their original value is converted by any use. This means that referencing a variable or constant can have unexpected side-effects. ๐ฑ For example, with historical implementations the following program:
{
a = "+2"
b = 2
if (NR % 2)
c = a + b
if (a == b)
print "numeric comparison"
else
print "string comparison"
}
would perform a numeric comparison (and output numeric comparison) for each odd-numbered line, but perform a string comparison (and output string comparison) for each even-numbered line. POSIX.1โ2008 ensures that comparisons will be numeric if necessary. With historical implementations, the following program:
BEGIN {
OFMT = "%e"
print 3.14
OFMT = "%f"
print 3.14
}
would output "3.140000e+00" twice, because in the second print statement the constant "3.14" would have a string value from the previous conversion. POSIX.1โ2008 requires that the output of the second print statement be "3.140000". The behavior of historical implementations was seen as too unintuitive and unpredictable. ๐
It was pointed out that with the rules contained in early drafts, the following script would print nothing:
BEGIN {
y[1.5] = 1
OFMT = "%e"
print y[1.5]
}
Therefore, a new variable, CONVFMT, was introduced. The OFMT variable is now restricted to affecting output conversions of numbers to strings and CONVFMT is used for internal conversions, such as comparisons or array indexing. The default value is the same as that for OFMT, so unless a program changes CONVFMT (which no historical program would do), it will receive the historical behavior associated with internal string conversions. ๐
The POSIX awk lexical and syntactic conventions are specified more formally than in other sources. Again, the intent has been to specify historical practice. One convention that may not be obvious from the formal grammar as in other verbal descriptions is where <newline> characters are acceptable. There are several obvious placements such as terminating a statement, and a <backslash> can be used to escape <newline> characters between any lexical tokens. In addition, <newline> characters without <backslash> characters can follow:
- a comma
- an open brace
- a logical AND operator (
"&&") - a logical OR operator (
"||") - the
dokeyword - the
elsekeyword - the closing parenthesis of an
if,for, orwhilestatement
For example:
{ print $1,
$2 }
The requirement that awk add a trailing <newline> to the program argument text is to simplify the grammar, making it match a text file in form. There is no way for an application or test suite to determine whether a literal <newline> is added or whether awk simply acts as if it did. ๐คซ
POSIX.1โ2008 requires several changes from historical implementations in order to support internationalization. Probably the most subtle of these is the use of the decimal-point character, defined by the LC_NUMERIC category of the locale, in representations of floating-point numbers. This locale-specific character is used in:
- recognizing numeric input
- converting between strings and numeric values
- formatting output
However โ regardless of locale โ the <period> character (the decimal-point character of the POSIX locale) is the decimal-point character recognized in processing awk programs (including assignments in command line arguments). This is essentially the same convention as the one used in the ISO C standard. The difference is that the C language includes the setlocale() function, which permits an application to modify its locale. Because of this capability, a C application begins executing with its locale set to the C locale, and only executes in the environment-specified locale after an explicit call to setlocale(). Adding such an elaborate new feature to the awk language was seen as inappropriate for POSIX.1โ2008. It is possible to execute an awk program explicitly in any desired locale by setting the environment in the shell. ๐ฆ
The undefined behavior resulting from NULs in extended regular expressions allows future extensions for the GNU gawk program to process binary data. ๐
The behavior in the case of invalid awk programs (including lexical, syntactic, and semantic errors) is undefined, because it was considered overly limiting on implementations to specify. In most cases such errors can be expected to produce a diagnostic and a non-zero exit status. However, some implementations may choose to extend the language in ways that make use of certain invalid constructs. Other invalid constructs might be deemed worthy of a warning, but otherwise cause some reasonable behavior. Still other constructs may be very difficult to detect in some implementations. Also, different implementations might detect a given error during an initial parsing of the program (before reading any input files) while others might detect it when executing the program after reading some input. Implementors should be aware that diagnosing errors as early as possible and producing useful diagnostics can ease debugging of applications, and thus make an implementation more usable. ๐ซถ
The unspecified behavior from using multi-character RS values is to allow possible future extensions based on extended regular expressions used for record separators. Historical implementations take the first character of the string and ignore the others.
Unspecified behavior when split(string,array,<null>) is used is to allow a proposed future extension that would split up a string into an array of individual characters. โจ
In the context of the getline function, equally good arguments for different precedences of the | and < operators can be made. Historical practice has been that:
getline < "a" "b"
is parsed as:
( getline < "a" ) "b"
although many would argue that the intent was that the file ab should be read. However:
getline < "x" + 1
parses as:
getline < ( "x" + 1 )
Similar problems occur with the | version of getline, particularly in combination with $. For example:
$"echo hi" | getline
(This situation is particularly problematic when used in a print statement, where the |getline part might be a redirection of the print.) ๐ญ๐
Since in most cases those kinds of constructs are not used โ or at least shouldn't be, bestie โ because they've got a natural ambiguity with no conventional parsing to save them, the meaning of these constructs has been made explicitly unspecified. ๐ And like, what that means in practice: a conforming application that runs into the problem must parenthesize to resolve the ambiguity. And honestly? There appeared to be few if any actual uses of such constructs, so nobody's crying about it.
Grammars can totally be written that would cause an error under these circumstances. So where backwards-compatibility is not a large consideration, implementors may wish to use such grammars. Slay, do your thing.
Some historical implementations have allowed some built-in functions to be called without an argument list, with the result being a default argument list chosen in some "reasonable" way. Using length as a synonym for length($0) is the only one of these forms that's thought to be widely known or widely used โ this particular form is documented in various places (for example, most historical awk reference pages, although not in the referenced The AWK Programming Language) as legitimate practice. With this exception, default argument lists have always been undocumented and vaguely defined, and it's not at all clear how (or if) they should be generalized to user-defined functions. They add no useful functionality and they preclude possible future extensions that might need to name functions without calling them. So not standardizing them seems the simplest course. ๐ซถ
That said, the standard developers considered that length merited special treatment, because it's been documented in the past and sees possibly substantial use in historical programs. Accordingly, this usage has been made legitimate โ but Issue 5 removed the obsolescent marking for XSI-conforming implementations and many otherwise conforming applications depend on this feature. iykyk.
In sub and gsub, if repl is a string literal (the lexical token STRING), then two consecutive <backslash> characters should be used in the string to ensure a single <backslash> will precede the <ampersand> when the resultant string is passed to the function. (For example, to specify one literal <ampersand> in the replacement string, use gsub(ERE, "\\&").) ๐ธ
Historically, the only special character in the repl argument of sub and gsub string functions was the <ampersand> ('&') character, and preceding it with the <backslash> character was used to turn off its special meaning.
The description in the ISO POSIXโ2:1993 standard introduced behavior such that the <backslash> character was another special character and it was unspecified whether there were any other special characters. That description introduced several portability problems, some of which are described below, and so it has been replaced with the more historical description. Some of the problems include:
- Historically, to create the replacement string, a script could use
gsub(ERE, "\\&"), but with the ISO POSIXโ2:1993 standard wording, it was necessary to usegsub(ERE, "\\\\&"). The<backslash>characters are doubled here because all string literals are subject to lexical analysis, which would reduce each pair of<backslash>characters to a single<backslash>before being passed togsub. - Since it was unspecified what the special characters were, for portable scripts to guarantee that characters are printed literally, each character had to be preceded with a
<backslash>. (For example, a portable script had to usegsub(ERE, "\\h\\i")to produce a replacement string of"hi".) ๐ญ
The description for comparisons in the ISO POSIXโ2:1993 standard did not properly describe historical practice because of the way numeric strings are compared as numbers. The current rules cause the following code:
if (0 == "000")
print "strange, but true"
else
print "not true"
to do a numeric comparison, causing the if to succeed. It should be intuitively obvious that this is incorrect behavior, and indeed, no historical implementation of awk actually behaves this way. not even one. ๐
To fix this problem, the definition of numeric string was enhanced to include only those values obtained from specific circumstances (mostly external sources) where it is not possible to determine unambiguously whether the value is intended to be a string or a numeric.
Variables that are assigned to a numeric string shall also be treated as a numeric string. (For example, the notion of a numeric string can be propagated across assignments.) In comparisons, all variables having the uninitialized value are to be treated as a numeric operand evaluating to the numeric value zero. โจ
Uninitialized variables include all types of variables including scalars, array elements, and fields. The definition of an uninitialized value in Variables and Special Variables is necessary to describe the value placed on uninitialized variables and on fields that are valid (for example, < $NF) but have no characters in them, and to describe how these variables are to be used in comparisons. A valid field, such as $1, that has no characters in it can be obtained from an input line of "\t\t" when FS='\t'. Historically, the comparison ($1<10) was done numerically after evaluating $1 to the value zero.
The phrase "โฆ also shall have the numeric value of the numeric string" was removed from several sections of the ISO POSIXโ2:1993 standard because it specifies an unnecessary implementation detail. It is not necessary for POSIX.1โ2008 to specify that these objects be assigned two different values. It is only necessary to specify that these objects may evaluate to two different values depending on context. ๐ฆ
Historical implementations of awk did not parse hexadecimal integer or floating constants like "0xa" and "0xap0". Due to an oversight, the 2001 through 2004 editions of this standard required support for hexadecimal floating constants. This was due to the reference to atof(). This version of the standard allows but does not require implementations to use atof() and includes a description of how floating-point numbers are recognized as an alternative to match historic behavior. The intent of this change is to allow implementations to recognize floating-point constants according to either the ISO/IEC 9899:1990 standard or ISO/IEC 9899:1999 standard, and to allow (but not require) implementations to recognize hexadecimal integer constants.
Historical implementations of awk did not support floating-point infinities and NaNs in numeric strings; e.g., "-INF" and "NaN". However, implementations that use the atof() or strtod() functions to do the conversion picked up support for these values if they used a ISO/IEC 9899:1999 standard version of the function instead of a ISO/IEC 9899:1990 standard version. Due to an oversight, the 2001 through 2004 editions of this standard did not allow support for infinities and NaNs, but in this revision support is allowed (but not required). This is a silent change to the behavior of awk programs; for example, in the POSIX locale the expression:
("-INF" + 0 < 0)
formerly had the value 0 because "-INF" converted to 0, but now it may have the value 0 or 1. ๐ silent behavior change, no warning, just vibes.
๐ฎ FUTURE DIRECTIONS ๐ฎ
A future version of this standard may require the "!=" and "==" operators to perform string comparisons by checking if the strings are identical (and not by checking if they collate equally). ๐
๐ SEE ALSO ๐
- Section 1.3, Grammar Conventions,
grep,lex,sed - The Base Definitions volume of POSIX.1โ2017, Chapter 5, File Format Notation, Section 6.1, Portable Character Set, Chapter 8, Environment Variables, Chapter 9, Regular Expressions, Section 12.2, Utility Syntax Guidelines
- The System Interfaces volume of POSIX.1โ2017,
atof(),exec,isspace(),popen(),setlocale(),strtod()
โ๏ธ COPYRIGHT โ๏ธ
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie โ we did it ๐
This one's from IEEE/The Open Group, dated 2017, filed under AWK(1POSIX). Go forth and awk responsibly, mwah ๐๐
๐ฆ where this page comes from
- debian bookworm: manpages-posix 2017a-2 ยท Other
- debian trixie: manpages-posix 2017a-2 ยท Other
- ubuntu jammy: manpages-posix 2017a-2 ยท Other
- ubuntu noble: manpages-posix 2017a-2 ยท Other
- ubuntu resolute: manpages-posix 2017a-2build1 ยท Other
- ubuntu stonking: manpages-posix 2017a-2.1 ยท Other
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms