gawk: Expression Patterns
1
1 7.1.2 Expressions as Patterns
1 -----------------------------
1
1 Any 'awk' expression is valid as an 'awk' pattern. The pattern matches
1 if the expression's value is nonzero (if a number) or non-null (if a
1 string). The expression is reevaluated each time the rule is tested
1 against a new input record. If the expression uses fields such as '$1',
1 the value depends directly on the new input record's text; otherwise, it
1 depends on only what has happened so far in the execution of the 'awk'
1 program.
1
1 Comparison expressions, using the comparison operators described in
1 ⇒Typing and Comparison, are a very common kind of pattern.
1 Regexp matching and nonmatching are also very common expressions. The
1 left operand of the '~' and '!~' operators is a string. The right
1 operand is either a constant regular expression enclosed in slashes
1 ('/REGEXP/'), or any expression whose string value is used as a dynamic
1 regular expression (⇒Computed Regexps). The following example
1 prints the second field of each input record whose first field is
1 precisely 'li':
1
1 $ awk '$1 == "li" { print $2 }' mail-list
1
1 (There is no output, because there is no person with the exact name
1 'li'.) Contrast this with the following regular expression match, which
1 accepts any record with a first field that contains 'li':
1
1 $ awk '$1 ~ /li/ { print $2 }' mail-list
1 -| 555-5553
1 -| 555-6699
1
1 A regexp constant as a pattern is also a special case of an
1 expression pattern. The expression '/li/' has the value one if 'li'
1 appears in the current input record. Thus, as a pattern, '/li/' matches
1 any record containing 'li'.
1
1 Boolean expressions are also commonly used as patterns. Whether the
1 pattern matches an input record depends on whether its subexpressions
1 match. For example, the following command prints all the records in
1 'mail-list' that contain both 'edu' and 'li':
1
1 $ awk '/edu/ && /li/' mail-list
1 -| Samuel 555-3430 samuel.lanceolis@shu.edu A
1
1 The following command prints all records in 'mail-list' that contain
1 _either_ 'edu' or 'li' (or both, of course):
1
1 $ awk '/edu/ || /li/' mail-list
1 -| Amelia 555-5553 amelia.zodiacusque@gmail.com F
1 -| Broderick 555-0542 broderick.aliquotiens@yahoo.com R
1 -| Fabius 555-1234 fabius.undevicesimus@ucb.edu F
1 -| Julie 555-6699 julie.perscrutabor@skeeve.com F
1 -| Samuel 555-3430 samuel.lanceolis@shu.edu A
1 -| Jean-Paul 555-2127 jeanpaul.campanorum@nyu.edu R
1
1 The following command prints all records in 'mail-list' that do _not_
1 contain the string 'li':
1
1 $ awk '! /li/' mail-list
1 -| Anthony 555-3412 anthony.asserturo@hotmail.com A
1 -| Becky 555-7685 becky.algebrarum@gmail.com A
1 -| Bill 555-1675 bill.drowning@hotmail.com A
1 -| Camilla 555-2912 camilla.infusarum@skynet.be R
1 -| Fabius 555-1234 fabius.undevicesimus@ucb.edu F
1 -| Martin 555-6480 martin.codicibus@hotmail.com A
1 -| Jean-Paul 555-2127 jeanpaul.campanorum@nyu.edu R
1
1 The subexpressions of a Boolean operator in a pattern can be constant
1 regular expressions, comparisons, or any other 'awk' expressions. Range
1 patterns are not expressions, so they cannot appear inside Boolean
1 patterns. Likewise, the special patterns 'BEGIN', 'END', 'BEGINFILE',
1 and 'ENDFILE', which never match any input record, are not expressions
1 and cannot appear inside Boolean patterns.
1
1 The precedence of the different operators that can appear in patterns
1 is described in ⇒Precedence.
1