The main input loop is executed as many times as there are lines of
input. As you saw in the "Hello, world" examples, this loop does not
execute until there is a line of input. It terminates when there is
no more input to be read.
Awk allows you to write two special routines that can be executed
before any input is read and
after all input is read. These are the
procedures associated with the BEGIN and
END rules, respectively. In other words, you can
do some preprocessing before the main input loop is ever executed and
you can do some postprocessing after the main input loop has
terminated. The BEGIN and END
procedures are optional.
Figure 7.1. Flow and control in awk scripts
Of these three parts, the main input loop or "what happens during
processing" is where most of the work gets done. Inside the main
input loop, your instructions are written as a series of
pattern/action procedures. A pattern is a rule for testing the input
line to determine whether or not the action should be applied to it.
The actions, as we shall see, can be quite complex, consisting of
statements, functions, and expressions.
The main thing to remember is that each pattern/action procedure sits
in the main input loop, which takes care of reading the input line.
The procedures that you write will be applied to each input line, one
line at a time.
 |  |  |
7.2. Hello, World |  | 7.4. Pattern Matching |