O'Reilly logo

Learning AWK Programming by Shiwang Kalkhanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using regular expressions as values of the FS

If a string is longer than a single character, it is considered as a regular expression while setting the FS. The FS can have any string containing any regular expression. Whenever the match is found for the regular expression, it separates the fields. Multiple FS can be specified within square brackets, as shown in the following example:

$ echo "a:b;c,d" | awk 'BEGIN { FS = "[:;,]"} ; {print $1, $2, $3, $4 }'

In the preceding example, we specify multiple FS as :, ;, and ,.

The output of the execution of this code is as follows:

a b c d

In our next example, the FS matches the hello phrase:

$ echo -e "Hey...! Hello Good Morning\nHey..! hello good morning" | awk 'BEGIN {FS="[Hh]ello"}{print $1}' ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required