Pattern Action Summary (The GNU Awk User’s Guide)
From Get docs
Gawk/docs/latest/Pattern-Action-Summary
Previous: Built-in Variables, Up: Patterns and Actions [Contents][Index]
7.6 Summary
- Pattern–action pairs make up the basic elements of an
awkprogram. 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
BEGINandENDrules has certain constraints. This is also true, only more so, forBEGINFILEandENDFILErules. 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
awkprograms by careful use of shell quoting. It is easier to pass a shell variable intoawkby using the-voption and anawkvariable. - 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
awkareif-else,while,for, anddo-while.gawkadds theswitchstatement. There are two flavors offorstatement: one for performing general looping, and the other for iterating through an array. breakandcontinuelet you exit early or start the next iteration of a loop (or get out of aswitch).nextandnextfilelet 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
exitstatement terminates your program. When executed from an action (or function body), it transfers control to theENDstatements. From anENDstatement 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 fromawkto your program. ARGCandARGVmake the command-line arguments available to your program. Manipulating them from aBEGINrule lets you control howawkwill process the provided data files.
Previous: Built-in Variables, Up: Patterns and Actions [Contents][Index]