AWK - Regular Expression Operators



This example explains the two forms of regular expressions operators.

Match

It is represented as ~. It looks for a field that contains the match string. For instance, the following example prints the lines that contain the pattern 9.

Example

[jerry]$ awk '$0 ~ 9' marks.txt

On executing this code, you get the following result −

Output

2) Rahul   Maths    90
5) Hari    History  89

Not Match

It is represented as !~. It looks for a field that does not contain the match string. For instance, the following example prints the lines that do not contain the pattern 9.

Example

[jerry]$ awk '$0 !~ 9' marks.txt

On executing this code, you get the following result −

Output

1) Amit     Physics   80
3) Shyam    Biology   87
4) Kedar    English   85
awk_operators.htm
Advertisements