Next: , Previous: , Up: Field Separators   [Contents][Index]


4.5.4 Setting FS from the Command Line

FS can be set on the command line. Use the -F option to do so. For example:

awk -F, 'program' input-files

sets FS to the ‘,’ character. Notice that the option uses an uppercase ‘F’ instead of a lowercase ‘f’. The latter option (-f) specifies a file containing an awk program.

The value used for the argument to -F is processed in exactly the same way as assignments to the predefined variable FS. Any special characters in the field separator must be escaped appropriately. For example, to use a ‘\’ as the field separator on the command line, you would have to type:

# same as FS = "\\"
awk -F\\\\ '…' files …

Because ‘\’ is used for quoting in the shell, awk sees ‘-F\\’. Then awk processes the ‘\\’ for escape characters (see Escape Sequences), finally yielding a single ‘\’ to use for the field separator.

As a special case, in compatibility mode (see Options), if the argument to -F is ‘t’, then FS is set to the TAB character. If you type ‘-F\t’ at the shell, without any quotes, the ‘\’ gets deleted, so awk figures that you really want your fields to be separated with TABs and not ‘t’s. Use ‘-v FS="t"’ or ‘-F"[t]"’ on the command line if you really do want to separate your fields with ‘t’s. Use ‘-F '\t'’ when not in compatibility mode to specify that TABs separate fields.

As an example, let’s use an awk program file called edu.awk that contains the pattern /edu/ and the action ‘print $1’:

/edu/   { print $1 }

Let’s also set FS to be the ‘-’ character and run the program on the file mail-list. The following command prints a list of the names of the people that work at or attend a university, and the first three digits of their phone numbers:

$ awk -F- -f edu.awk mail-list
-| Fabius       555
-| Samuel       555
-| Jean

Note the third line of output. The third line in the original file looked like this:

Jean-Paul    555-2127     jeanpaul.campanorum@nyu.edu     R

The ‘-’ as part of the person’s name was used as the field separator, instead of the ‘-’ in the phone number that was originally intended. This demonstrates why you have to be careful in choosing your field and record separators.

Perhaps the most common use of a single character as the field separator occurs when processing the Unix system password file. On many Unix systems, each user has a separate entry in the system password file, with one line per user. The information in these lines is separated by colons. The first field is the user’s login name and the second is the user’s encrypted or shadow password. (A shadow password is indicated by the presence of a single ‘x’ in the second field.) A password file entry might look like this:

arnold:x:2076:10:Arnold Robbins:/home/arnold:/bin/bash

The following program searches the system password file and prints the entries for users whose full name is not indicated:

awk -F: '$5 == ""' /etc/passwd

Next: , Previous: , Up: Field Separators   [Contents][Index]