Expressions Summary (The GNU Awk User’s Guide)
From Get docs
- Expressions are the basic elements of computation in programs. They are built from constants, variables, function calls, and combinations of the various kinds of values with operators.
awksupplies three kinds of constants: numeric, string, and regexp.
gawklets you specify numeric constants in octal and hexadecimal (bases 8 and 16) as well as decimal (base 10). In certain contexts, a standalone regexp constant such as
/foo/has the same meaning as ‘
$0 ~ /foo/’.
- Variables hold values between uses in computations. A number of built-in variables provide information to your
awkprogram, and a number of others let you control how
- Numbers are automatically converted to strings, and strings to numbers, as needed by
awk. Numeric values are converted as if they were formatted with
sprintf()using the format in
CONVFMT. Locales can influence the conversions.
awkprovides the usual arithmetic operators (addition, subtraction, multiplication, division, modulus), and unary plus and minus. It also provides comparison operators, Boolean operators, an array membership testing operator, and regexp matching operators. String concatenation is accomplished by placing two expressions next to each other; there is no explicit operator. The three-operand ‘
?:’ operator provides an “if-else” test within expressions.
- Assignment operators provide convenient shorthands for common arithmetic operations.
awk, a value is considered to be true if it is nonzero or non-null. Otherwise, the value is false.
- A variable’s type is set upon each assignment and may change over its lifetime. The type determines how it behaves in comparisons (string or numeric).
- Function calls return a value that may be used as part of a larger expression. Expressions used to pass parameter values are fully evaluated before the function is called.
awkprovides built-in and user-defined functions; this is described in Functions.
- Operator precedence specifies the order in which operations are performed, unless explicitly overridden by parentheses.
awk’s operator precedence is compatible with that of C.
- Locales can affect the format of data as output by an
awkprogram, and occasionally the format for data read as input.