Professional Documents
Culture Documents
One at a time, and in order, awk compares each input record with the pattern of
every rule in the program. When a pattern matches, awk performs the action part
of the rule on that input record. Patterns and actions often refer to separate f
ields within a record. By default, white space (usually blanks, newlines, or hor
izontal tab characters) separates fields; however, you can specify a different f
ield separator string using the -F ere option (see Input).
You can omit the pattern or action part of an awk rule (but not both). If you om
it pattern, awk performs the action on every input record (that is, every record
matches). If you omit action, awk writes every record matching the pattern to t
he standard output.
awk considers everything after a # in a program line to be a comment. For exampl
e:
# This is a comment
To continue program lines on the next line, add a backslash (\) to the end of th
e line. Statement lines ending with a comma (,), double or-bars (||), or double
ampersands (&&) continue automatically on the next line.
Options
-F ere
specifies an extended regular expression to use as the field separator.
-f prog
runs the awk program contained in the file prog. When more than one -f option ap
pears on the command line, the resulting program is a concatenation of all progr
ams you specify.
-v var=value
assigns value to var before running the program. You can specify this option a n
umber of times.
Variables and Expressions
There are three types of variables in awk: identifiers, fields and array element
s.
An identifier is a sequence of letters, digits and underscores beginning with a
letter or an underscore.
For a description of fields, see the Input subsection.
Arrays are associative collections of values called the elements of the array. C
onstructs of the form,
identifier[subscript]
where subscript has the form expr or expr,expr,.... refer to array elements. Eac
h such expr can have any string value. For multiple expr subscripts, awk concate
nates the string values of all exprs with a separate character SUBSEP between ea
ch. The initial value of SUBSEP is set to \034 (ASCII field separator).
Fields and identifiers are sometimes referred to as scalar variables to distingu
ish them from arrays.
You do not declare awk variables and you do not need to initialize them. The val
ue of an uninitialized variable is the empty string in a string context and the
number 0 in a numeric context.
Expressions consist of constants, variables, functions, regular expressions and
SYMTAB["var"]
is a synonym for the variable var.
Environment
An awk program can determine its initial environment by examining the ENVIRON ar
ray. If the environment consists of entries of the form:
name=value
then
ENVIRON[name]
has string value
"value"
For example, the following program is equivalent to the default output of env:
BEGIN {
for (i in ENVIRON)
printf("%s=%s\n", i, ENVIRON[i])
exit
}
Operators
awk follows the usual precedence order of arithmetic operations, unless overridd
en with parentheses; a table giving the order of operations appears later in thi
s section.
The unary operators are +, -, ++ and --, where you can use the ++ and -- operato
rs as either postfix or prefix operators, as in C. The binary arithmetic operato
rs are +, -, *, /, % and ^.
The conditional operator
expr ? expr1 : expr2
evaluates to expr1 if the value of expr is non-zero, and to expr2 otherwise.
If two expressions are not separated by an operator, awk concatenates their stri
ng values.