AWK is very powerful and efficient in handling regular expressions. A number of complex tasks can be solved with simple regular expressions. Any command-line expert knows the power of regular expressions.
This chapter covers standard regular expressions with suitable examples.
It matches any single character except the end of line character. For instance, the following example matches fin, fun, fan etc.
[jerry]$ echo -e "cat\nbat\nfun\nfin\nfan" | awk '/f.n/'
On executing the above code, you get the following result −
fun fin fan
It matches the start of line. For instance, the following example prints all the lines that start with pattern The.
[jerry]$ echo -e "This\nThat\nThere\nTheir\nthese" | awk '/^The/'
On executing this code, you get the following result −
There Their
It matches the end of line. For instance, the following example prints the lines that end with the letter n.
[jerry]$ echo -e "knife\nknow\nfun\nfin\nfan\nnine" | awk '/n$/'
On executing this code, you get the following result −
fun fin fan
It is used to match only one out of several characters. For instance, the following example matches pattern Call and Tall but not Ball.
[jerry]$ echo -e "Call\nTall\nBall" | awk '/[CT]all/'
On executing this code, you get the following result −
Call Tall
In exclusive set, the carat negates the set of characters in the square brackets. For instance, the following example prints only Ball.
[jerry]$ echo -e "Call\nTall\nBall" | awk '/[^CT]all/'
On executing this code, you get the following result −
Ball
A vertical bar allows regular expressions to be logically ORed. For instance, the following example prints Ball and Call.
[jerry]$ echo -e "Call\nTall\nBall\nSmall\nShall" | awk '/Call|Ball/'
On executing this code, you get the following result −
Call Ball
It matches zero or one occurrence of the preceding character. For instance, the following example matches Colour as well as Color. We have made u as an optional character by using ?.
[jerry]$ echo -e "Colour\nColor" | awk '/Colou?r/'
On executing this code, you get the following result −
Colour Color
It matches zero or more occurrences of the preceding character. For instance, the following example matches ca, cat, catt, and so on.
[jerry]$ echo -e "ca\ncat\ncatt" | awk '/cat*/'
On executing this code, you get the following result −
ca cat catt
It matches one or more occurrence of the preceding character. For instance below example matches one or more occurrences of the 2.
[jerry]$ echo -e "111\n22\n123\n234\n456\n222" | awk '/2+/'
On executing the above code, you get the following result −
22 123 234 222
Parentheses () are used for grouping and the character | is used for alternatives. For instance, the following regular expression matches the lines containing either Apple Juice or Apple Cake.
[jerry]$ echo -e "Apple Juice\nApple Pie\nApple Tart\nApple Cake" | awk '/Apple (Juice|Cake)/'
On executing this code, you get the following result −
Apple Juice Apple Cake