gawk: Command Line Field Separator
1
1 4.5.4 Setting 'FS' from the Command Line
1 ----------------------------------------
1
1 'FS' can be set on the command line. Use the '-F' option to do so. For
1 example:
1
1 awk -F, 'PROGRAM' INPUT-FILES
1
1 sets 'FS' to the ',' character. Notice that the option uses an
1 uppercase 'F' instead of a lowercase 'f'. The latter option ('-f')
1 specifies a file containing an 'awk' program.
1
1 The value used for the argument to '-F' is processed in exactly the
1 same way as assignments to the predefined variable 'FS'. Any special
1 characters in the field separator must be escaped appropriately. For
1 example, to use a '\' as the field separator on the command line, you
1 would have to type:
1
1 # same as FS = "\\"
1 awk -F\\\\ '...' files ...
1
1 Because '\' is used for quoting in the shell, 'awk' sees '-F\\'. Then
11 'awk' processes the '\\' for escape characters (⇒Escape
Sequences), finally yielding a single '\' to use for the field
1 separator.
1
1 As a special case, in compatibility mode (⇒Options), if the
1 argument to '-F' is 't', then 'FS' is set to the TAB character. If you
1 type '-F\t' at the shell, without any quotes, the '\' gets deleted, so
1 'awk' figures that you really want your fields to be separated with TABs
1 and not 't's. Use '-v FS="t"' or '-F"[t]"' on the command line if you
1 really do want to separate your fields with 't's. Use '-F '\t'' when
1 not in compatibility mode to specify that TABs separate fields.
1
1 As an example, let's use an 'awk' program file called 'edu.awk' that
1 contains the pattern '/edu/' and the action 'print $1':
1
1 /edu/ { print $1 }
1
1 Let's also set 'FS' to be the '-' character and run the program on
1 the file 'mail-list'. The following command prints a list of the names
1 of the people that work at or attend a university, and the first three
1 digits of their phone numbers:
1
1 $ awk -F- -f edu.awk mail-list
1 -| Fabius 555
1 -| Samuel 555
1 -| Jean
1
1 Note the third line of output. The third line in the original file
1 looked like this:
1
1 Jean-Paul 555-2127 jeanpaul.campanorum@nyu.edu R
1
1 The '-' as part of the person's name was used as the field separator,
1 instead of the '-' in the phone number that was originally intended.
1 This demonstrates why you have to be careful in choosing your field and
1 record separators.
1
1 Perhaps the most common use of a single character as the field
1 separator occurs when processing the Unix system password file. On many
1 Unix systems, each user has a separate entry in the system password
1 file, with one line per user. The information in these lines is
1 separated by colons. The first field is the user's login name and the
1 second is the user's encrypted or shadow password. (A shadow password
1 is indicated by the presence of a single 'x' in the second field.) A
1 password file entry might look like this:
1
1 arnold:x:2076:10:Arnold Robbins:/home/arnold:/bin/bash
1
1 The following program searches the system password file and prints
1 the entries for users whose full name is not indicated:
1
1 awk -F: '$5 == ""' /etc/passwd
1