Patterns

A pattern can be any of the following:

general expression
/regular expression/
relational expression
pattern-matching expression
BEGIN
END
  • General expressions can be composed of quoted strings, numbers, operators, function calls, user-defined variables, or any of the predefined variables described in Built-in Variables in Built-in Variables.

  • Regular expressions use the extended set of metacharacters, as described in Chapter 7.

  • The ^ and $ metacharacters refer to the beginning and end of a string (such as the fields), respectively, rather than the beginning and end of a line. In particular, these metacharacters will not match at a newline embedded in the middle of a string.

  • Relational expressions use the relational operators listed in Operators in Operators. For example, $2 > $1 selects lines for which the second field is greater than the first. Comparisons can be either string or numeric. Thus, depending upon the types of data in $1 and $2, awk will do either a numeric or a string comparison. This can change from one record to the next.

  • Pattern-matching expressions use the operators ~ (match) and !~ (don’t match). See Operators in Operators.

  • The BEGIN pattern lets you specify procedures that will take place before the first input line is processed. (Generally, you process the command line and set global variables here.)

  • The END pattern lets you specify procedures that will take place after the last input record is read.

  • BEGIN and END patterns may appear multiple times. The procedures ...

Get Linux in a Nutshell, 6th 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.