Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Here, you only print those lines that have less than the specified number of fruit.

Assuming this script is called reorder.sh, executing the script as follows

$ ./reorder.sh 25

produces the output

Kiwi            $1.50           22

Flow Control

There are three main forms for flow control in awk:

  The if statement
  The while statement
  The for statement

The if and while statements are similar to the versions in the shell, whereas the for statement is much closer to the version found in the C language version.

You will look at each of these statements in turn.

The if Statement

The if statement enables you to make tests before executing some awk command.

The pattern matching and expressions that you have used in the previous examples are essentially if statements that affect the overall execution of the awk program. The if statement should be used within an action rather than in the main input processing loop.

The basic syntax of the if statement is

if (expression1) {
    action1
} else if (expression2) {
    action2
} else {
    action3
}

Here expression1 and expression2 are expressions created using the conditional operators. They are identical to expressions you looked at earlier in the chapter. The parentheses surrounding expression1 and expression2 are required.

The actions—action1, action2, and action3—can be any sequence of valid awk commands. The braces surrounding these actions are required only when an action contains more than one statement, but I recommend that you always use them for the sake of clarity and maintainability.

Both the else if and the else statements are optional. There is no limit on the number of else if statements that can be given.

The execution is as follows:

1.  Evaluate expression1 (if).
2.  If expression1 is true, execute action1 and exit the if statement.
3.  If expression1 is false, evaluate expression2 (else if).
4.  If expression2 is true, execute action2 and exit the if statement.
5.  If expression2 is false, execute action3 and exit the if statement (else).

As a simple example, write a script that prints a list of fruit in fruit_prices.txt highlighting the following facts:

  Whether an item costs more than a dollar
  Whether you need to reorder the item

Fruit that costs more than a dollar is highlighted with the * character. Fruit that needs to be reordered because its quantity is less than 75 is highlighted with the string REORDER.

Using the if statement, the script becomes

#!/bin/sh

awk '{
    printf "%s\t",$0;

    if ( $2 ~ /\$[1-9][0-9]*\.[0-9][0-9]/) {

        printf " * ";
        if ( $3 <= 75) {
            printf "REORDER\n" ;
        } else {
            printf "\n" ;
        }
    } else {
        if ( $3 < 75) {
            printf "   REORDER\n" ;
        } else {
            printf "\n" ;
        }

    }
}' fruit_prices.txt ;

The output looks like the following

Fruit           Price/lbs       Quantity
Banana          $0.89           100
Peach           $0.79           65         REORDER
Kiwi            $1.50           22       * REORDER
Pineapple       $1.29           35       * REORDER
Apple           $0.99           78

This example also shows you how to nest if statements.

The while Statement

The while statement executes awk commands while an expression is true. The basic syntax is

while (expression) {
    actions
}

Here expression is an expression created using the conditional operators. It is identical to expressions you looked at earlier in the chapter. The parentheses surrounding expression are required.

The actions that should be performed, actions, are any sequence of valid awk commands. The braces surrounding the actions are required only for actions containing more than one statement, but I recommend that you always use them for the sake of clarity and maintainability.

Here is a simple example of the while loop that counts from one to five:

$ awk 'BEGIN{ x=0 ; while (x < 5) { x+=1 ; print x ; } }'

The output looks like the following:

1
2
3
4
5

The do Statement A variation on the while statement is the do statement. It also performs some actions while an expression is true.

The basic syntax is

do {
    actions
} while (expression)

Here expression is an expression created using the conditional operators. It is identical to expressions you looked at earlier in the chapter. The parentheses surrounding expression are required.

The actions that should be performed, actions, are any sequence of valid awk commands. The braces surrounding the actions are required only for actions containing more than one statement, but I recommend that you always use them for the sake of clarity and maintainability.

The main difference is that the do statement executes at least once, whereas the while statement might not execute at all. For example, you can write the while loop in the previous example as the following do loop:

$ awk 'BEGIN { 'BEGIN{ x=0 ; do { x+=1 ; print x ; } while (x < 5) }'

There are slight variations between nawk, gawk, and awk with regard to this do statement. If you want to use the statement, you should stick to nawk or gawk because older versions of awk might have trouble with it. If you are concerned with portability to older versions of UNIX, you should avoid using the do statement.


Previous Table of Contents Next