BEGIN and END Rules
There are several (sometimes subtle) issues involved when doing I/O
from a BEGIN or END rule.
The first has to do with the value of $0 in a BEGIN
rule. Since BEGIN rules are executed before any input is read,
there simply is no input record, and therefore no fields, when
executing BEGIN rules. References to $0 and the fields
yield a null string or zero, depending upon the context. One way
to give $0 a real value is to execute a getline command
without a variable (see section Explicit Input with getline).
Another way is to simply assign a value to it.
The second point is similar to the first, but from the other direction.
Inside an END rule, what is the value of $0 and NF?
Traditionally, due largely to implementation issues, $0 and
NF were undefined inside an END rule.
The POSIX standard specified that NF was available in an END
rule, containing the number of fields from the last input record.
Due most probably to an oversight, the standard does not say that $0
is also preserved, although logically one would think that it should be.
In fact, gawk does preserve the value of $0 for use in
END rules. Be aware, however, that Unix awk, and possibly
other implementations, do not.
The third point follows from the first two. What is the meaning of
`print' inside a BEGIN or END rule? The meaning is
the same as always, `print $0'. If $0 is the null string,
then this prints an empty line. Many long time awk programmers
use `print' in BEGIN and END rules, to mean
`print ""', relying on $0 being null. While you might
generally get away with this in BEGIN rules, in gawk at
least, it is a very bad idea in END rules. It is also poor
style, since if you want an empty line in the output, you
should say so explicitly in your program.
Go to the first, previous, next, last section, table of contents.