Pattern Matching

The “Hello, world” program does not demonstrate the power of pattern-matching rules. In this section, we look at a number of small, even trivial examples that nonetheless demonstrate this central feature of awk scripts.

When awk reads an input line, it attempts to match each pattern-matching rule in a script. Only the lines matching the particular pattern are the object of an action. If no action is specified, the line that matches the pattern is printed (executing the print statement is the default action). Consider the following script:

/^$/ { print "This is a blank line." }

This script reads: if the input line is blank, then print “This is a blank line.” The pattern is written as a regular expression that identifies a blank line. The action, like most of those we’ve seen so far, contains a single print statement.

If we place this script in a file named awkscr and use an input file named test that contains three blank lines, then the following command executes the script:

$ awk -f awkscr test
This is a blank line.
This is a blank line.
This is a blank line.

(From this point on, we’ll assume that our scripts are placed in a separate file and invoked using the -f command-line option.) The result tells us that there are three blank lines in test. This script ignores lines that are not blank.

Let’s add several new rules to the script. This script is now going to analyze the input and classify it as an integer, a string, or a blank line.

# test for integer, string or empty ...

Get sed & awk, 2nd Edition now with the O’Reilly learning platform.

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