Gawk/Pattern-Action-Summary
From Get docs
Previous: Built-in Variables, Up: Patterns and Actions [Contents][Index]
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 keywordsBEGIN
,END
,BEGINFILE
, orENDFILE
; or empty. The action executes if the current record matches the pattern. Empty (missing) patterns match all records. - I/O from
BEGIN
andEND
rules has certain constraints. This is also true, only more so, forBEGINFILE
andENDFILE
rules. The latter two give you “hooks” intogawk
’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 intoawk
by using the-v
option and anawk
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
areif
-else
,while
,for
, anddo
-while
.gawk
adds theswitch
statement. There are two flavors offor
statement: one for performing general looping, and the other for iterating through an array. break
andcontinue
let you exit early or start the next iteration of a loop (or get out of aswitch
).next
andnextfile
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 theEND
statements. From anEND
statement body, it exits immediately. You may pass an optional numeric value to be used asawk
’s exit status. - Some predefined variables provide control over
awk
, mainly for I/O. Other variables convey information fromawk
to your program. ARGC
andARGV
make the command-line arguments available to your program. Manipulating them from aBEGIN
rule lets you control howawk
will process the provided data files.
Previous: Built-in Variables, Up: Patterns and Actions [Contents][Index]