The input field separator

We have already discussed that an input field separator is a whitespace by default. We can change this IFS to other values on the command line or by using the BEGIN statement. We need to use the -F option to change the IFS.

This is an example:

    $ cat people.txt
  

The output will be as follows:

    Bill Thomas:8000:08/9/1968
    Fred Martin:6500:22/7/1982
    Julie Moore:4500:25/2/1978
    Marie Jones:6000:05/8/1972
    Tom Walker:7000:14/1/1977
    $ awk -F: '/Marie/{print $1, $2}' people.txt
  

The output will be as follows:

    Marie Jones 6000
  

We have used the -F option to specify colon (:) as IFS instead of the default, IFS. Therefore, it has printed field 1 and 2 of the records in which the Marie pattern was matched. We can even specify ...

Get Learning Linux Shell Scripting - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.