Regular expression operators

When we use the == condition, AWK looks for an exact match. However, when we use the match operator (~), AWK looks for a partial match. Here, ~ means contains. To match a specific pattern using regular expression, ~ and !~ are used. Regular expression comparisons are performed using a matching expression built with either of these two operators. The right-hand side of the ~ or !~ operator could be a regular expression or string enclosed between forward slashes (/…/).

Match operator (~):

It is represented as a tilde (~) symbol. It matches a pattern in a specific field. Its syntax is as follows:

expression ~ /regexpr/

It matches if the string value of the expression contains a sub-string matched by regular expression regexpr.

For example, if you want to print the records containing Singh or Kapur in last name field from the employees database emp.dat, use the ~ operator as follows:

$ awk '$2 ~ /(Singh|Kapur)/ { print }' emp.dat

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

Jack    Singh   9857532312  [email protected]      M   hr      2000
Julie Kapur 8826234556 [email protected] F Ops 2500
Hari Singh 8827255666 [email protected] M Ops 2350
John Kapur 9911556789 [email protected] M hr 2200
Ginny Singh 9857123466 [email protected] F hr 2250
Vina Singh 8811776612 [email protected] F lgs 2300

In the following example, we print the total number of users who have bash as their default login shell. In this AWK command, if the last field of a line contains the pattern bash, the AWK variable n gets incremented by one:

$ awk -F ':' '$NF ~ /bash/{n++};END { print n}' /etc/passwd

The output on execution of the preceding code is:

2 

Not match operator (!~):

It is represented by !~. It is the opposite of the ~ operator. It matches the fields that do not contain the specified pattern. Its syntax is as follows:

expression !~ /regexpr/

It matches if the string value of the expression does not contain a sub-string matched by regular expression regexpr.

For example, if you want to print the records not containing Singh or Kapur in the last name field from the employees database emp.dat, use !~ as follows:

$ awk '$2 !~ /(Singh|Kapur)/ { print }' emp.dat

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

Jane    Kaur    9837432312  [email protected]      F   hr      1800
Eva Chabra 8827232115 [email protected] F lgs 2100
Amit Sharma 9911887766 [email protected] M lgs 2350
Ana Khanna 9856422312 [email protected] F Ops 2700
Victor Sharma 8826567898 [email protected] M Ops 2500
Billy Chabra 9911664321 [email protected] M lgs 1900
Sam khanna 8856345512 [email protected] F lgs 2300
Emily Kaur 8826175812 [email protected] F Ops 2100
Amy Sharma 9857536898 [email protected] F Ops 2500

Regular expression operators:

Operator

Description

~

Match operator

!~

No match operator

 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.161.225