Actions in awk
Last revision August 6, 2004
Table of Contents: |
Actions consist of one or more statements enclosed within curly braces ({}).
Each statement should begin on a new line, or be separated from the previous statement by a semi-colon if on the same line.
Wherever a single statement is required, multiple statements can be substituted if they are enclosed within a set of matching braces, that is {...}.
The simplest statement is to print, or write to the output stream. In fact, the default action if none is given is simply to print the input line to the output.
The print statement can take any number of arguments that are the names of variables, or constant strings of characters enclosed in double quotes ("), or any combination thereof. If no arguments are given, then the print statement will simply print the entire current line (the one that matches the corresponding pattern for this action).
If the variable names or constant strings are separated by blanks, their contents will be concatenated on the output, without any intervening blanks.
If the variable names or constant strings are separated by commas, their contents will be separated on the output by the value of the "output field separator" variable, which is normally a blank (you can change its value by a statement like OFS = x, in a BEGIN action, for example).
Examples of actions:
{print NR, $0}
Prints each line with its line number at the beginning.
{print $2, $1}
Prints just the first two fields of each line, in reverse order.
END {print "The last line number =" NR, "with", NF, "fields"}
Use with the END pattern to show last line number and number
of its fields.
There is also a printf statement exactly like that in the C language (type man 3 printf on pangea for the syntax) which lets you control the precise formatting of numeric data and locations of fields on the output line.
Other action statements can assign or manipulate variables, usually for later printing. Examples:
sum += $1
Add the value of field $1 in the input record to the existing value
of the variable sum. This is equivalent to the statement sum
= sum + 1.
avg = sum / NR
Average value of the $1 field accumulated in the sum
statement above. Use this statement in the END action.
Flow of control in actions
Complete flow-of-control statements like those in the C language are available to conditionally or repetitively execute statements in the action, instead of just executing a simple list of action statements.
if statements
The if statement provides a way to execute one or more actions only if a certain logical expression (variables combined with logical operators) is true or false. Syntax:
-
if (logical_expression)
statement
else statement
The logical_expression in parentheses is evaluated to see if it is true. If it is, then the statement on the next line (or on same line) is executed. If not, then the statement following the else keyword is executed, if given; otherwise the next statement following the whole if construct is executed.
if statements can be nested.
Any statement can be a set of statements spanning multiple lines if they are enclosed in matching braces.
Example:
-
if ($1 > 100)
print "found big value =" $1 " at line " NR
else sum+=$1
while statements
The while statement repetitively executes the following statement as long as its logical expression is true. Syntax:
-
while (logical_expression) statement
Generally, the logical_expression includes a variable that is incremented or updated by the statement, so that it eventually will no longer be true and the while statement will stop looping.
The statement that is executed can be many separate statements enclosed in a set of braces.
Example:
-
i=0
while (i < NF) {
i+=1
print $i
}
for statements
The for statement implements loops exactly as does the C language.
It really accomplishes the same thing as the while statement, but
in a more compact syntax. It includes three expressions: one to initialize one
or more variables; the second to test a logical expression,
usually involving the initialized variables; and the third to
increment or update the variables before the next test. The syntax for the
for statement is:
for (exp1 ; logical_exp ; exp3) statement
This is the equivalent of the following set of statements using while:
-
exp1
while (logical_exp) {
statement
exp3
}
As in the case of the if and while statements, the single statement following the for command can be expanded to a set of statements enclosed in braces.