gawk: Regexp Usage
1
1 3.1 How to Use Regular Expressions
1 ==================================
1
1 A regular expression can be used as a pattern by enclosing it in
1 slashes. Then the regular expression is tested against the entire text
1 of each record. (Normally, it only needs to match some part of the text
1 in order to succeed.) For example, the following prints the second
1 field of each record where the string 'li' appears anywhere in the
1 record:
1
1 $ awk '/li/ { print $2 }' mail-list
1 -| 555-5553
1 -| 555-0542
1 -| 555-6699
1 -| 555-3430
1
1 Regular expressions can also be used in matching expressions. These
1 expressions allow you to specify the string to match against; it need
1 not be the entire current input record. The two operators '~' and '!~'
1 perform regular expression comparisons. Expressions using these
1 operators can be used as patterns, or in 'if', 'while', 'for', and 'do'
1 statements. (⇒Statements.) For example, the following is true
1 if the expression EXP (taken as a string) matches REGEXP:
1
1 EXP ~ /REGEXP/
1
1 This example matches, or selects, all input records with the uppercase
1 letter 'J' somewhere in the first field:
1
1 $ awk '$1 ~ /J/' inventory-shipped
1 -| Jan 13 25 15 115
1 -| Jun 31 42 75 492
1 -| Jul 24 34 67 436
1 -| Jan 21 36 64 620
1
1 So does this:
1
1 awk '{ if ($1 ~ /J/) print }' inventory-shipped
1
1 This next example is true if the expression EXP (taken as a character
1 string) does _not_ match REGEXP:
1
1 EXP !~ /REGEXP/
1
1 The following example matches, or selects, all input records whose
1 first field _does not_ contain the uppercase letter 'J':
1
1 $ awk '$1 !~ /J/' inventory-shipped
1 -| Feb 15 32 24 226
1 -| Mar 15 24 34 228
1 -| Apr 31 52 63 420
1 -| May 16 34 29 208
1 ...
1
1 When a regexp is enclosed in slashes, such as '/foo/', we call it a
1 "regexp constant", much like '5.27' is a numeric constant and '"foo"' is
1 a string constant.
1