A pattern in front of an action acts as a selector that determines whether the action is to be executed. A variety of expressions may be used as patterns: regular expressions, arithmetic relational expressions, string-valued expressions, and arbitrary boolean combinations of these.
The special pattern BEGIN matches the beginning of the input, before the first record is read. The pattern END matches the end of the input, after the last record has been processed. BEGIN and END thus provide a way to gain control before and after processing, for initialization and wrapup.
As an example, the field separator can be set to a colon by
The simplest regular expression is a literal string of characters enclosed in slashes, like
Awk regular expressions include the regular expression forms found in the UNIX text editor ed unix program manual and grep (without back-referencing). In addition, awk allows parentheses for grouping, | for alternatives, + for ``one or more'', and ? for ``zero or one'', all as in lex. Character classes may be abbreviated: [a-zA-Z0-9] is the set of all letters and digits. As an example, the awk program
Regular expressions (with the extensions listed above) must be enclosed in slashes, just as in ed and sed. Within a regular expression, blanks and the regular expression metacharacters are significant. To turn of the magic meaning of one of the regular expression characters, precede it with a backslash. An example is the pattern
One can also specify that any field or variable matches a regular expression (or does not match it) with the operators ~ and !~. The program
An awk pattern can be a relational expression involving the usual relational operators <, <=, ==, !=, >=, and >. An example is
In relational tests, if neither operand is numeric, a string comparison is made; otherwise it is numeric. Thus,
A pattern can be any boolean combination of patterns, using the operators || (or), && (and), and ! (not). For example,
The ``pattern'' that selects an action may also consist of two patterns separated by a comma, as in