Gawk/Expression-Patterns
Next: Ranges, Previous: Regexp Patterns, Up: Pattern Overview [Contents][Index]
7.1.2 Expressions as Patterns
Any awk
expression is valid as an awk
pattern.
The pattern matches if the expression’s value is nonzero (if a
number) or non-null (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 on only what has happened so far in the execution of the
awk
program.
Comparison expressions, using the comparison operators described in
Variable Typing and Comparison Expressions,
are a very common kind of pattern.
Regexp matching and nonmatching are also very common expressions.
The left operand of the ‘~
’ and ‘!~
’ operators is a string.
The right operand is either a constant regular expression enclosed in
slashes (/regexp/
), or any expression whose string value
is used as a dynamic regular expression
(see section Using Dynamic Regexps).
The following example prints the second field of each input record
whose first field is precisely ‘li
’:
$ awk '$1 == "li" { print $2 }' mail-list
(There is no output, because there is no person with the exact name ‘li
’.)
Contrast this with the following regular expression match, which
accepts any record with a first field that contains ‘li
’:
$ awk '$1 ~ /li/ { print $2 }' mail-list -| 555-5553 -| 555-6699
A regexp constant as a pattern is also a special case of an expression
pattern. The expression /li/
has the value one if ‘li
’
appears in the current input record. Thus, as a pattern, /li/
matches any record containing ‘li
’.
Boolean expressions are also commonly used as patterns.
Whether the pattern
matches an input record depends on whether its subexpressions match.
For example, the following command prints all the records in
mail-list
that contain both ‘edu
’ and ‘li
’:
$ awk '/edu/ && /li/' mail-list -| Samuel 555-3430 [email protected] A
The following command prints all records in
mail-list
that contain either ‘edu
’ or ‘li
’
(or both, of course):
$ awk '/edu/ || /li/' mail-list -| Amelia 555-5553 [email protected] F -| Broderick 555-0542 [email protected] R -| Fabius 555-1234 [email protected] F -| Julie 555-6699 [email protected] F -| Samuel 555-3430 [email protected] A -| Jean-Paul 555-2127 [email protected] R
The following command prints all records in
mail-list
that do not contain the string ‘li
’:
$ awk '! /li/' mail-list -| Anthony 555-3412 [email protected] A -| Becky 555-7685 [email protected] A -| Bill 555-1675 [email protected] A -| Camilla 555-2912 [email protected] R -| Fabius 555-1234 [email protected] F
-| Martin 555-6480 [email protected] A -| Jean-Paul 555-2127 [email protected] R
The subexpressions of a Boolean operator in a pattern can be constant regular
expressions, comparisons, or any other awk
expressions. Range
patterns are not expressions, so they cannot appear inside Boolean
patterns. Likewise, the special patterns BEGIN
, END
,
BEGINFILE
, and ENDFILE
,
which never match any input record, are not expressions and cannot
appear inside Boolean patterns.
The precedence of the different operators that can appear in patterns is described in Operator Precedence (How Operators Nest).
Next: Ranges, Previous: Regexp Patterns, Up: Pattern Overview [Contents][Index]