Learning AWK Programming
上QQ阅读APP看书,第一时间看更新

Patterns

In pattern-action statements, the pattern is something that determines when an action is to be executed. We can summarize the usage of patterns as follows: 

  • BEGIN { statements }: The statements are executed once before any input has been read.
  • END { statements }: The statements are executed once after all input has been read.
  • expression { statements }: The statements are executed at each input line where the expression is true, that is, non-zero or non-null.
  • / regular expression / { statements }: The statements are executed at each input line that contains a string matched by the regular expression.
  • compound pattern { statements }: A compound pattern combines expressions with && (AND), || (OR), ! (NOT), and parentheses; the statements are executed at each input line where the compound pattern is true.
  • pattern 1, pattern 2 { statements }: A range pattern matches each input line from a line matched by pattern 1 to the next line matched by pattern 2, inclusive; the statements are executed at each matching line. Here, the pattern range could be of regular expressions or addresses.

BEGIN and END do not combine with other patterns. A range pattern cannot be part of any other pattern. BEGIN and END are the only patterns that require an action.