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

Alternation operator

The alternation operator is used to specify alternatives. The alternation operator is a vertical bar or pipe symbol, it matches one regular expression out of several regular expressions. The '|' alternation operator has the lowest precedence among all the regular expression operators. For example, in the employee database, if we want to print the lines that have first name of the employee as EmilyJack, or Ana, we can write the following regular expression:

$ awk '/Emily|Jack|Ana/' emp.dat

In AWK, it is generally used with the ( ) grouping operator, as follows:

$ awk '/(Emily|Jack|Ana)/' emp.dat

The output on execution of the given code is as follows:

Jack    Singh   9857532312  jack@gmail.com      M   hr      2000Ana Khanna 9856422312 ...

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