manpagez: man pages & more
info gawk
Home | html | info | man

gawk: Expression Patterns

 
 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
 ⇒Typing and Comparison, 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 (⇒Computed 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     samuel.lanceolis@shu.edu        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     amelia.zodiacusque@gmail.com    F
      -| Broderick    555-0542     broderick.aliquotiens@yahoo.com R
      -| Fabius       555-1234     fabius.undevicesimus@ucb.edu    F
      -| Julie        555-6699     julie.perscrutabor@skeeve.com   F
      -| Samuel       555-3430     samuel.lanceolis@shu.edu        A
      -| Jean-Paul    555-2127     jeanpaul.campanorum@nyu.edu     R
 
    The following command prints all records in 'mail-list' that do _not_
 contain the string 'li':
 
      $ awk '! /li/' mail-list
      -| Anthony      555-3412     anthony.asserturo@hotmail.com   A
      -| Becky        555-7685     becky.algebrarum@gmail.com      A
      -| Bill         555-1675     bill.drowning@hotmail.com       A
      -| Camilla      555-2912     camilla.infusarum@skynet.be     R
      -| Fabius       555-1234     fabius.undevicesimus@ucb.edu    F
      -| Martin       555-6480     martin.codicibus@hotmail.com    A
      -| Jean-Paul    555-2127     jeanpaul.campanorum@nyu.edu     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 ⇒Precedence.
 
© manpagez.com 2000-2018
Individual documents may contain additional copyright information.