There are several (sometimes subtle) points to remember when doing I/O
The first has to do with the value of
$0 in a
BEGIN rules are executed before any input is read,
there simply is no input record, and therefore no fields, when
BEGIN rules. References to
$0 and the fields
yield a null string or zero, depending upon the context. One way
$0 a real value is to execute a
without a variable (see Getline).
Another way is simply to assign a value to
The second point is similar to the first but from the other direction.
Traditionally, due largely to implementation issues,
NF were undefined inside an
The POSIX standard specifies that
NF is available in an
rule. It contains the number of fields from the last input record.
Most probably due to an oversight, the standard does not say that
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 Brian Kernighan's awk, and possibly
other implementations, do not.
The third point follows from the first two. The meaning of ‘print’
END rule is the same as always:
‘print $0’. If
$0 is the null string, then this prints an
empty record. Many long time awk programmers use an unadorned
END rules, to mean ‘print ""’,
$0 being null. Although one might generally get away with
BEGIN rules, it is a very bad idea in
at least in gawk. It is also poor style, since if an empty
line is needed in the output, the program should print one explicitly.
nextfile statements are not allowed
BEGIN rule, because the implicit
read-a-record-and-match-against-the-rules loop has not started yet. Similarly, those statements
are not valid in an
END rule, since all the input has been read.
(See Next Statement, and see