-->
Previous | Table of Contents | Next |
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. Heres 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 havent encountered programming languages before, but it is the same as the for loop used in C, for example.
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.
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 dont have to declare the array or do anything special with it, which is one of the powerful features of gawk.
Weve only scratched the surface of gawks abilities, but you may have noticed that it is a relatively easy language to work with and places no special demands on the programmer. Thats 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:
Previous | Table of Contents | Next |