7.6 Summary ¶
- Pattern–action pairs make up the basic elements of an
awk
program. Patterns are either normal expressions, range expressions,
or regexp constants; one of the special keywords BEGIN, END,
BEGINFILE, or ENDFILE; or empty. The action executes if
the current record matches the pattern. Empty (missing) patterns match
all records.
- I/O from
BEGIN and END rules has certain constraints.
This is also true, only more so, for BEGINFILE and ENDFILE
rules. The latter two give you “hooks” into gawk’s file
processing, allowing you to recover from a file that otherwise would
cause a fatal error (such as a file that cannot be opened).
- Shell variables can be used in
awk programs by careful
use of shell quoting. It is easier to pass a shell variable into
awk by using the -v option and an awk
variable.
- Actions consist of statements enclosed in curly braces. Statements
are built up from expressions, control statements, compound statements,
input and output statements, and deletion statements.
- The control statements in
awk are if-else,
while, for, and do-while. gawk
adds the switch statement. There are two flavors of for
statement: one for performing general looping, and the other for iterating
through an array.
break and continue let you exit early or start the next
iteration of a loop (or get out of a switch).
next and nextfile let you read the next record and start
over at the top of your program or skip to the next input file and
start over, respectively.
- The
exit statement terminates your program. When executed
from an action (or function body), it transfers control to the
END statements. From an END statement body, it exits
immediately. You may pass an optional numeric value to be used
as awk’s exit status.
- Some predefined variables provide control over
awk, mainly for I/O.
Other variables convey information from awk to your program.
ARGC and ARGV make the command-line arguments available
to your program. Manipulating them from a BEGIN rule lets you
control how awk will process the provided data files.