DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with awk

Control flow statements

awk provides if-else, while, do-while, and for statements, and statement grouping with braces, as in the C programming language.

The if statement syntax is

if (expression) statement[1] else statement[2]

The expression acting as the conditional has no restrictions; it can include the relational operators <, <=, >, >=, ==, and !=; the extended regular expression matching operators ~ and !~; the logical operators ||, &&, and !; juxtaposition for concatenation; and parentheses for grouping.

In the if statement, awk first evaluates the expression. If it is non-zero and non-null, statement[1] is executed; otherwise statement[2] is executed. The else part is optional.

A single statement can always be replaced by a statement list enclosed in braces. The statements in the statement list are terminated by newlines or semicolons.

Rewriting the maximum population program from the ``Arithmetic functions'' section with an if statement results in

   {    if (maxpop < $3) {
             maxpop = $3
             country = $1
        }
   }
   END  { print country, maxpop }
The while statement is exactly that of the C programming language:
   while (expression) statement

The expression is evaluated; if it is non-zero and non-null the statement is executed and the expression is tested again. The cycle repeats as long as the expression is non-zero. For example, to print all input fields one per line,

   {  i = 1
      while (i <= NF) {
         print $i
         i++
      }
   }

The for statement is like that of the C programming language:

   for (expression[1]; expression; expression[2]) statement
It has the same effect as
   expression[1]
   while (expression) {
   	statement
   	expression[2]
   }
so
   { for (i = 1; i <= NF; i++)  print $i }
does the same job as the while example shown above. An alternate version of the for statement is described in the next section.

The do statement has the form

   do statement while (expression)
The statement is executed repeatedly until the value of the expression becomes zero. Because the test takes place after the execution of the statement (at the bottom of the loop), it is always executed at least once. As a result, the do statement is used much less often than while or for, which test for completion at the top of the loop.

The following example of a do statement prints all lines except those between start and stop.

   /start/ {
              do {
                   getline x
              } while (x !~ /stop/)
           }
           { print }

The break statement causes an immediate exit from an enclosing while or for; the continue statement causes the next iteration to begin. The next statement causes awk to skip immediately to the next record and begin matching patterns starting from the first pattern-action statement.

The exit statement causes the program to behave as if the end of the input had occurred; no more input is read, and the END action, if any, is executed. Within the END action,

   exit expr
causes the program to return the value of expr as its exit status. If there is no expr, the exit status is zero.
Next topic: Arrays
Previous topic: Number or string?

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005