Using regular expressions with AWK
There are mainly two types of regular expressions in Linux:
- Basic regular expressions that are used by vi, sed, grep, and so on
- Extended regular expressions that are used by awk, nawk, gawk, and egrep
Here, we will refer to extended regular expressions as regular expressions in the context of AWK.
In AWK, regular expressions are enclosed in forward slashes, '/', (forming the AWK pattern) and match every input record whose text belongs to that set.
The simplest regular expression is a string of letters, numbers, or both that matches itself. For example, here we use the ly regular expression string to print all lines that contain the ly pattern in them. We just need to enclose the regular expression in forward slashes in AWK:
$ awk '/ly/' emp.dat
The output on execution of this code is as follows:
Billy Chabra 9911664321 bily@yahoo.com M lgs 1900
Emily Kaur 8826175812 emily@gmail.com F Ops 2100
In this example, the /ly/ pattern matches when the current input line contains the ly sub-string, either as ly itself or as some part of a bigger word, such as Billy or Emily, and prints the corresponding line.