home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

sed & awk

sed & awkSearch this book
Previous: 8.2 Looping Chapter 8
Conditionals, Loops, and Arrays
Next: 8.4 Arrays

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

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 ) 
	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.[1] 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:

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

FILENAME == "acronyms" {

{ 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.

Previous: 8.2 Looping sed & awk Next: 8.4 Arrays
8.2 Looping Book Index 8.4 Arrays

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System