/^$/ { 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 line.
/[0-9]+/ { print "That is an integer" }
/[A-Za-z]+/ { print "This is a string" }
/^$/ { print "This is a blank line." }