According to the POSIX standard, awk
is supposed to behave
as if each record is split into fields at the time that it is read.
In particular, this means that you can change the value of FS
after a record is read, and the value of the fields (i.e. how they were split)
should reflect the old value of FS
, not the new one.
However, many implementations of awk
do not work this way. Instead,
they defer splitting the fields until a field is actually
referenced. The fields will be split
using the current value of FS
! (d.c.)
This behavior can be difficult
to diagnose. The following example illustrates the difference
between the two methods.
(The sed
(6)
command prints just the first line of `/etc/passwd'.)
sed 1q /etc/passwd | awk '{ FS = ":" ; print $1 }'
will usually print
root
on an incorrect implementation of awk
, while gawk
will print something like
root:nSijPlPhZZwgE:0:0:Root:/:
The following table summarizes how fields are split, based on the
value of FS
. (`==' means "is equal to.")
FS == " "
FS == any other single character
FS == regexp
FS == ""
Go to the first, previous, next, last section, table of contents.