manpagez: man pages & more
info gawk
Home | html | info | man
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [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. Case is significant in command-line options: the ‘-F’ and ‘-f’ options have nothing to do with each other. You can use both options at the same time to set the FS variable and get an awk program from a file.

The value used for the argument to ‘-F’ is processed in exactly the same way as assignments to the built-in 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 section Escape Sequences), finally yielding a single ‘\’ to use for the field separator.

As a special case, in compatibility mode (see section Command-Line 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.

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

 
/300/   { print $1 }

Let’s also set FS to be the ‘-’ character and run the program on the file ‘BBS-list’. The following command prints a list of the names of the bulletin boards that operate at 300 baud and the first three digits of their phone numbers:

 
$ awk -F- -f baud.awk BBS-list
-| aardvark     555
-| alpo
-| barfly       555
-| bites        555
-| camelot      555
-| core         555
-| fooey        555
-| foot         555
-| macfoo       555
-| sdace        555
-| sabafoo      555

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

 
alpo-net     555-3412     2400/1200/300     A

The ‘-’ as part of the system’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, 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 password file entry might look like this:

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

The following program searches the system password file and prints the entries for users who have no password:

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

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]
© manpagez.com 2000-2024
Individual documents may contain additional copyright information.