The first example contains a single instruction that prints the first
field of each line in the input file.
$ awk '{ print $1 }' list
John
Alice
Orville
Terry
Eric
Hubert
Amy
Sal
"$1" refers to the value of the first field on each input line.
Because there is no pattern specified, the print statement is applied
to all lines. In the next example, a pattern "/MA/" is
specified but there is no procedure. The default action is to print
each line that matches the pattern.
$ awk '/MA/' list
John Daggett, 341 King Road, Plymouth MA
Eric Adams, 20 Post Road, Sudbury MA
Sal Carpenter, 73 6th Street, Boston MA
Three lines are printed. As mentioned in the first chapter, an awk
program can be used more like a query language, extracting useful
information from a file. We might say that the pattern placed a
condition on the selection of records to be included in a report,
namely that they must contain the string "MA". Now we can also
specify what portion of a record to include in the report. The
next example uses a print statement to limit the
output to the first field of each record.
$ awk '/MA/ { print $1 }' list
John
Eric
Sal