start page | rating of books | rating of authors | reviews | copyrights

sed & awksed & awkSearch this book

8.3. Other Statements That Affect Flow Control

The if, while, for, and do statements allow you to change the normal flow through a procedure. In this section, we look at several other statements that also affect a change in flow control.

There are two statements that affect the flow control of a loop, break and continue. The break statement, as you'd expect, breaks out of the loop, such that no more iterations of the loop are performed. The continue statement stops the current iteration before reaching the bottom of the loop and starts a new iteration at the top.

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.

There are two statements that affect the main input loop, next and exit. The next statement causes the next line of input to be read and then resumes execution at the top of the script.[55] This allows you to avoid applying other procedures on the current input line. A typical use of the next statement is to continue reading input from a file, ignoring the other actions in the script until that file is exhausted. The system variable FILENAME provides the name of the current input file. Thus, a pattern can be written:

[55]Some awks don't allow you to use next from within a user-defined function; Caveat emptor.

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.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.