An Awk Primer/Control Structures

Awk supports control structures similar to those used in C, including: There are also control structures different from those used in C:
 * - loop in array
 * - go to the next input record
 * - stop processing the input files, execute END operations, if specified, and finish.

if ... else
The syntax of "if ... else" is: if (&lt;condition&gt;) &lt;action 1&gt; [else &lt;action 2&gt;] The "else" clause is optional. The "condition" can be any expression discussed in the section on pattern matching, including matches with regular expressions. For example, consider the following Awk program: {   if ($1=="green") print "GO"; else if ($1=="yellow") print "SLOW DOWN"; else if ($1=="red") print "STOP"; else print "SAY WHAT?"; } (Here, semicolons are optional and can be omitted.)

By the way, for test purposes this program can be invoked as: echo "red" | awk -f pgm.txt —where "pgm.txt" is a text file containing the program.

The "action" clauses can consist of multiple statements, contained by curly brackets ("{}").

while
The syntax for "while" is: while (&lt;condition&gt;) &lt;action&gt; The "action" is performed as long the "condition" tests true, and the

"condition" is tested before each iteration. The conditions are the same as for the "if ... else" construct. For example, since by default an Awk variable has a value of 0, the following Awk program could print the numbers from 1 to 20: BEGIN {x=0; while(x&lt;=20) {++x; print x}}

for
for (&lt;initial action&gt;;&lt;condition&gt;;&lt;end-of-loop action&gt;) &lt;action&gt;
 * The "for" loop is more flexible. It has the syntax:

For example, the following "for" loop prints the numbers 10 through 20 in increments of 2: BEGIN {for (i=10; i&lt;=20; i+=2) print i} This is equivalent to: i=10 while (i&lt;=20) { print i       i+=2 } The C programming language has a similar "for" construct, with an interesting feature in that multiple actions can be taken in both the initialization and end-of-loop actions, simply by separating the actions with a comma. Most implementations of Awk, unfortunately, do not support this feature.

The "for" loop has an alternate syntax, used when scanning through an array: for (&lt;variable&gt; in &lt;array&gt;) &lt;action&gt; Given the example used earlier:

my_string = "joe:frank:harry:bill:bob:sil";   split(my_string, names, ":"); —then the names could be printed with the following statement: for (idx in names) print idx, names[idx]; This yields: 2 frank 3 harry 4 bill 5 bob 6 sil 1 joe Notice that the names are not printed in the proper order. One of the characteristics of this type of "for" loop is that the array is not scanned in a predictable order.

Unconditional control statements
Awk defines four unconditional control statements: ,  ,  , and.

The statements  and   are strictly associated with the   and   loops:
 * break: Causes a jump out of the loop.
 * continue: Forces the next iteration of the loop.

The statements  and   control Awk's input scanning:
 * next: Causes Awk to immediately get another line of input and begin scanning it from the first match statement.
 * exit: Causes Awk to stop reading its input, execute END operations, if specified, and finish.