Patterns and Procedures
gawk scripts consist of patterns and procedures:
pattern {procedure}
Both are optional. If pattern is missing, { procedure } is applied to all records. If { procedure } is missing, the matched record is printed. By default, each line of input is a record, but you can specify a different record separator through the RS variable.
Patterns
A pattern can be any of the following:
/regular expression/ relational expression pattern-matching expression pattern,pattern BEGIN END
Some rules regarding patterns include:
Expressions can be composed of quoted strings, numbers, operators, functions, defined variables, or any of the predefined variables described later under “gawk System Variables.”
Regular expressions use the extended set of metacharacters and are described in Chapter 9.
In addition, ^ and $ can be used to refer to the beginning and end of a field, respectively, rather than the beginning and end of a record.
Relational expressions use the relational operators listed under “Operators” later in this chapter. Comparisons can be either string or numeric. For example, $2 > $1 selects lines for which the second field is greater than the first.
Pattern-matching expressions use the operators ~ (match) and !~ (don’t match). See “Operators” later in this chapter.
The BEGIN pattern lets you specify procedures that take place before the first input record is processed. (Generally, you set global variables here.)
The END pattern lets you specify procedures that ...
Get Linux in a Nutshell, Fourth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.