GNU Info

Info Node: (gawk.info)Expression Patterns

(gawk.info)Expression Patterns


Next: Ranges Prev: Regexp Patterns Up: Pattern Overview
Enter node , (file) or (file)node

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
Note: Variable Typing and Comparison Expressions,
are a very common kind of pattern.  Regexp matching and non-matching
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 (Note: Using
Dynamic Regexps.).  The following example prints the
second field of each input record whose first field is precisely `foo':

     $ awk '$1 == "foo" { print $2 }' BBS-list

(There is no output, because there is no BBS site with the exact name
`foo'.)  Contrast this with the following regular expression match,
which accepts any record with a first field that contains `foo':

     $ awk '$1 ~ /foo/ { print $2 }' BBS-list
     -| 555-1234
     -| 555-6699
     -| 555-6480
     -| 555-2127

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

   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
`BBS-list' that contain both `2400' and `foo':

     $ awk '/2400/ && /foo/' BBS-list
     -| fooey        555-1234     2400/1200/300     B

   The following command prints all records in `BBS-list' that contain
_either_ `2400' or `foo' (or both, of course):

     $ awk '/2400/ || /foo/' BBS-list
     -| alpo-net     555-3412     2400/1200/300     A
     -| bites        555-1675     2400/1200/300     A
     -| fooey        555-1234     2400/1200/300     B
     -| foot         555-6699     1200/300          B
     -| macfoo       555-6480     1200/300          A
     -| sdace        555-3430     2400/1200/300     A
     -| sabafoo      555-2127     1200/300          C

   The following command prints all records in `BBS-list' that do _not_
contain the string `foo':

     $ awk '! /foo/' BBS-list
     -| aardvark     555-5553     1200/300          B
     -| alpo-net     555-3412     2400/1200/300     A
     -| barfly       555-7685     1200/300          A
     -| bites        555-1675     2400/1200/300     A
     -| camelot      555-0542     300               C
     -| core         555-2912     1200/300          C
     -| sdace        555-3430     2400/1200/300     A

   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' and
`END', which never match any input record, are not expressions and
cannot appear inside Boolean patterns.


automatically generated by info2www version 1.2.2.9