Go to the first, previous, next, last section, table of contents.


Expressions as Patterns

Any awk expression is also valid as an awk pattern. Then the pattern "matches" if the expression's value is nonzero (if a number) or nonnull (if a string).

The expression is reevaluated each time the rule is tested against a new input record. If the expression uses fields such as $1, the value depends directly on the new input record's text; otherwise, it depends only on what has happened so far in the execution of the awk program, but that may still be useful.

Comparison patterns are actually a special case of this. For example, the expression $5 == "foo" has the value 1 when the value of $5 equals "foo", and 0 otherwise; therefore, this expression as a pattern matches when the two values are equal.

Boolean patterns are also special cases of expression patterns.

A constant regexp as a pattern is also a special case of an expression pattern. /foo/ as an expression has the value 1 if `foo' appears in the current input record; thus, as a pattern, /foo/ matches any record containing `foo'.

Other implementations of awk that are not yet POSIX compliant are less general than gawk: they allow comparison expressions, and boolean combinations thereof (optionally with parentheses), but not necessarily other kinds of expressions.


Go to the first, previous, next, last section, table of contents.