-->
Previous Table of Contents Next


The for Loop

The for loop is commonly used when you want to initialize a value and then ignore it. The syntax of the gawk for loop is


for (initialization; expression; increment) {

 command

   }

The initialization is executed only once and then ignored, the expression is evaluated each time the loop executes, and the increment is executed each time the loop is executed. Usually the increment is a counter of some type, but it can be any collection of valid commands. Here’s an example of a for loop, which is the same basic program as shown for the while loop:


# interest calculation computes compound interest

# inputs from a file are the amount, interest_rate, and years

{for (var=1; var <= $3; var++) {

   printf(“%f\n”, $1*(1+$2)^var)

   }

}

In this case, var is initialized when the for loop starts. The expression is evaluated, and if true, the loop runs. Then the value of var is incremented and the expression is tested again.

The format of the for loop may look strange if you haven’t encountered programming languages before, but it is the same as the for loop used in C, for example.

next and exit

The next instruction tells gawk to process the next record in the file, regardless of what it is doing. For example, consider this script:


{ command1

     command2

     command3

       next

     command4

}

As soon as the next statement is read, gawk moves to the next record in the file and starts at the top of the current script block (given by the curly brace). In this example, command4 will never be executed because the next statement moves back up to command1 each time.

The next statement is usually used inside an if loop, where you may want execution to return to the start of the script if some condition is met.

The exit statement makes gawk behave as though it has reached the end of the file, and it then executes any END patterns (if any exist). This is a useful method of aborting processing if there is an error in the file.

Arrays

The gawk language supports arrays and enables you to access any element in the array easily. No special initialization is necessary with an array, because gawk treats it like any other variable. The general format for declaring arrays is as follows:


var[num]=value

As an example, consider the following script, which reads an input file and generates an output file with the lines reversed in order:


# reverse lines in a file

{line[NR] = $0 } # remember each line

END {var=NR       # output lines in reverse order

     while (var > 0){

     print line[var]

     var--

     }

}

In this simple program (try and do the same task in any other programming language to see how efficient gawk is!), we use the NR (number of records) built-in variable. After reading each line into the array line[], we simply start at the last record and print them again, stepping down through the array each time. We don’t have to declare the array or do anything special with it, which is one of the powerful features of gawk.

Summary

We’ve only scratched the surface of gawk’s abilities, but you may have noticed that it is a relatively easy language to work with and places no special demands on the programmer. That’s one of the reasons gawk is so often used for quick programs. It is ideal, for example, for writing a quick script to count the total size of all the files in a directory. In the C language, this would take many lines, but it can be done in less than a dozen lines in gawk.

If you are a system administrator or simply a power user, you will find that gawk is a great complement to all the other tools you have available, especially because it can accept input from a pipe or redirection. For more information on gawk, check the man pages or one of the few gawk books that are available.

See the following chapters for information on programming:

C under Linux is discussed in Chapter 26, “Programming in C.”
Perl, another handy utility included with Linux, is discussed in Chapter 28, “Perl.”
Tcl and Tk, yet another programming language with extra features for X and Motif, is discussed in Chapter 29, “Introduction to Tcl and Tk.”


Previous Table of Contents Next