An Awk Primer/Control Structures

Awk supports control structures similar to those used in C, including:

  • if ... else
  • while
  • for
  • break
  • continue

There are also control structures different from those used in C:

  • for (x in a) - loop in array
  • next - go to the next input record
  • exit - stop processing the input files, execute END operations, if specified, and finish.

if ... elseEdit

The syntax of "if ... else" is:

   if (<condition>) <action 1> [else <action 2>]

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 ("{}").


The syntax for "while" is:

   while (<condition>) <action>

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<=20) {++x; print x}}


  • The "for" loop is more flexible. It has the syntax:
   for (<initial action>;<condition>;<end-of-loop action>) <action>

For example, the following "for" loop prints the numbers 10 through 20 in increments of 2:

   BEGIN {for (i=10; i<=20; i+=2) print i}

This is equivalent to:

    while (i<=20) {
        print i

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 (<variable> in <array>) <action>

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 statementsEdit

Awk defines four unconditional control statements: break, continue, next, and exit.

The statements break and continue are strictly associated with the while and for loops:

  • break: Causes a jump out of the loop.
  • continue: Forces the next iteration of the loop.

The statements next and exit 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.