It is important to remember that when you assign a string constant
as the value of FS
, it undergoes normal awk string
processing. For example, with Unix awk and gawk,
the assignment `FS = "\.."' assigns the character string ".."
to FS
(the backslash is stripped). This creates a regexp meaning
“fields are separated by occurrences of any two characters.”
If instead you want fields to be separated by a literal period followed
by any single character, use `FS = "\\.."'.
The following table summarizes how fields are split, based on the value
of FS
(`==' means “is equal to”):
FS == " "
FS ==
any other single characterFS ==
regexpFS == ""
FS
Does Not Affect the FieldsAccording to the POSIX standard, awk is supposed to behave
as if each record is split into fields at the time it is read.
In particular, this means that if you change the value of FS
after a record is read, 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 are 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 sed1
command prints just the first line of /etc/passwd.)
sed 1q /etc/passwd | awk '{ FS = ":" ; print $1 }'
which usually prints:
root
on an incorrect implementation of awk, while gawk prints something like:
root:nSijPlPhZZwgE:0:0:Root:/:
FS
and IGNORECASE
The IGNORECASE
variable
(see User-modified)
affects field splitting only when the value of FS
is a regexp.
It has no effect when FS
is a single character, even if
that character is a letter. Thus, in the following code:
FS = "c" IGNORECASE = 1 $0 = "aCa" print $1
The output is `aCa'. If you really want to split fields on an
alphabetic character while ignoring case, use a regexp that will
do it for you. E.g., `FS = "[c]"'. In this case, IGNORECASE
will take effect.
[1] The sed utility is a “stream editor.” Its behavior is also defined by the POSIX standard.