5.7. Regular Expressions

A regular expression to awk is a pattern that consists of characters enclosed in forward slashes. Awk supports the use of regular expression metacharacters (same as egrep) to modify the regular expression in some way. If a string in the input line is matched by the regular expression, the resulting condition is true, and any actions associated with the expression are executed. If no action is specified and an input line is matched by the regular expression, the record is printed. See Table 5.6.

Example 5.26.
% awk  '/Mary/'   employees
					Mary Adams  5346   11/4/63  28765
				

Explanation

Awk will display all lines in the employees file containing the regular expression pattern Mary.

Example 5.27.
% awk '/Mary/{print $1, $2}' ...

Get Linux Shells by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.