Consider what happens in the following program fragment:
for ( x = 1; x <= NF; ++x )
if ( y == $x ) {
print x, $x
break
}
print
A loop is set up to examine each field of the current input record.
Each time through the loop, the value of y is
compared to the value of a field referenced as $x.
If the result is true, we print the field number and its value and
then break from the loop. The next statement to be executed is
print. The use of break means
that we are interested only in the first match on a line and that we
don't want to loop through the rest of the fields.
Here's a similar example using the continue statement:
for ( x = 1; x <= NF; ++x ) {
if ( x == 3 )
continue
print x, $x
}
This example loops through the fields of the current input record,
printing the field number and its value. However (for some reason),
we want to avoid printing the third field. The conditional statement
tests the counter variable and if it is equal to 3, the
continue statement is executed. The
continue statement passes control back to the top
of the loop where the counter variable is incremented again. It
avoids executing the print statement for that
iteration. The same result could be achieved by simply re-writing the
conditional to execute print as long as
x is not equal to 3. The point is that you can use
the continue statement to avoid hitting the bottom
of the loop on a particular iteration.
FILENAME == "acronyms" {
action
next
}
{ print }
This causes the action to be performed for each line in the file
acronyms. After the action is performed, the
next line of input is read. Control does not pass to the
print statement until the input is taken from a
different source.
The exit statement exits the main input loop and
passes control to the END rule, if there is one.
If the END rule is not defined, or the
exit statement is used in the
END rule, then the script terminates. We used the
exit statement earlier in the
factorial program to exit after reading one line of
input.
An exit statement can take an expression as an
argument. The value of this expression will be returned as the exit
status of awk. If the expression is not supplied, the exit status is
0. If you supply a value to an initial exit
statement, and then call exit again from the
END rule without a value, the first value is
used. For example:
awk '{
...
exit 5
}
END { exit }'
Here, the exit status from awk will be 5.
You will come across examples that use these flow-control statements
in upcoming sections.