http://www.usf.uos.de/infoservice/doc/

(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.  Then the pattern
matches if the expression's value is non-zero (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 only on what has happened so far in the execution of the `awk'
program, but that may still be useful.

   A very common kind of expression used as a pattern is the comparison
expression, using the comparison operators described in Note: Variable
Typing and Comparison Expressions.

   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, since there is no BBS site named "foo".) Contrast
this with the following regular expression match, which would accept
any record with a first field that contains `foo':

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

   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 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.

     $ 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.

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


Next: Ranges Prev: Regexp Patterns Up: Pattern Overview
[ Dokumentation lokal installierter Software ]