gawk: Fields

1 
1 4.2 Examining Fields
1 ====================
1 
1 When 'awk' reads an input record, the record is automatically "parsed"
1 or separated by the 'awk' utility into chunks called "fields".  By
1 default, fields are separated by "whitespace", like words in a line.
1 Whitespace in 'awk' means any string of one or more spaces, TABs, or
1 newlines; other characters that are considered whitespace by other
1 languages (such as formfeed, vertical tab, etc.)  are _not_ considered
1 whitespace by 'awk'.
1 
1    The purpose of fields is to make it more convenient for you to refer
1 to these pieces of the record.  You don't have to use them--you can
1 operate on the whole record if you want--but fields are what make simple
1 'awk' programs so powerful.
1 
1    You use a dollar sign ('$') to refer to a field in an 'awk' program,
1 followed by the number of the field you want.  Thus, '$1' refers to the
1 first field, '$2' to the second, and so on.  (Unlike in the Unix shells,
1 the field numbers are not limited to single digits.  '$127' is the 127th
1 field in the record.)  For example, suppose the following is a line of
1 input:
1 
1      This seems like a pretty nice example.
1 
1 Here the first field, or '$1', is 'This', the second field, or '$2', is
1 'seems', and so on.  Note that the last field, '$7', is 'example.'.
1 Because there is no space between the 'e' and the '.', the period is
1 considered part of the seventh field.
1 
1    'NF' is a predefined variable whose value is the number of fields in
1 the current record.  'awk' automatically updates the value of 'NF' each
1 time it reads a record.  No matter how many fields there are, the last
1 field in a record can be represented by '$NF'.  So, '$NF' is the same as
1 '$7', which is 'example.'.  If you try to reference a field beyond the
1 last one (such as '$8' when the record has only seven fields), you get
1 the empty string.  (If used in a numeric operation, you get zero.)
1 
1    The use of '$0', which looks like a reference to the "zeroth" field,
1 is a special case: it represents the whole input record.  Use it when
1 you are not interested in specific fields.  Here are some more examples:
1 
1      $ awk '$1 ~ /li/ { print $0 }' mail-list
1      -| Amelia       555-5553     amelia.zodiacusque@gmail.com    F
1      -| Julie        555-6699     julie.perscrutabor@skeeve.com   F
1 
1 This example prints each record in the file 'mail-list' whose first
1 field contains the string 'li'.
1 
1    By contrast, the following example looks for 'li' in _the entire
1 record_ and prints the first and last fields for each matching input
1 record:
1 
1      $ awk '/li/ { print $1, $NF }' mail-list
1      -| Amelia F
1      -| Broderick R
1      -| Julie F
1      -| Samuel A
1