gawk: Full Line Fields
1
1 4.5.5 Making the Full Line Be a Single Field
1 --------------------------------------------
1
1 Occasionally, it's useful to treat the whole input line as a single
1 field. This can be done easily and portably simply by setting 'FS' to
1 '"\n"' (a newline):(1)
1
1 awk -F'\n' 'PROGRAM' FILES ...
1
1 When you do this, '$1' is the same as '$0'.
1
1 Changing 'FS' Does Not Affect the Fields
1
1 According to the POSIX standard, 'awk' is supposed to behave as if
1 each record is split into fields at the time it is read. In particular,
1 this means that if you change the value of 'FS' after a record is read,
1 the values of the fields (i.e., how they were split) should reflect the
1 old value of 'FS', not the new one.
1
1 However, many older implementations of 'awk' do not work this way.
1 Instead, they defer splitting the fields until a field is actually
1 referenced. The fields are split using the _current_ value of 'FS'!
1 (d.c.) This behavior can be difficult to diagnose. The following
1 example illustrates the difference between the two methods:
1
1 sed 1q /etc/passwd | awk '{ FS = ":" ; print $1 }'
1
1 which usually prints:
1
1 root
1
1 on an incorrect implementation of 'awk', while 'gawk' prints the full
1 first line of the file, something like:
1
1 root:x:0:0:Root:/:
1
1 (The 'sed'(2) command prints just the first line of '/etc/passwd'.)
1
1 ---------- Footnotes ----------
1
1 (1) Thanks to Andrew Schorr for this tip.
1
1 (2) The 'sed' utility is a "stream editor." Its behavior is also
1 defined by the POSIX standard.
1