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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.