Go to the first, previous, next, last section, table of contents.


Making Each Character a Separate Field

There are times when you may want to examine each character of a record separately. This can be done in @command{gawk} by simply assigning the null string ("") to FS. In this case, each individual character in the record becomes a separate field. For example:

$ echo a b | gawk 'BEGIN { FS = "" }
>                  {
>                      for (i = 1; i <= NF; i = i + 1)
>                          print "Field", i, "is", $i
>                  }'
-| Field 1 is a
-| Field 2 is
-| Field 3 is b

Traditionally, the behavior of FS equal to "" was not defined. In this case, most versions of Unix @command{awk} simply treat the entire record as only having one field. (d.c.) In compatibility mode (see section Command-Line Options), if FS is the null string, then @command{gawk} also behaves this way.


Go to the first, previous, next, last section, table of contents.