GNU Info

Info Node: (gawk.info)Regexp Usage

(gawk.info)Regexp Usage


Next: Escape Sequences Prev: Regexp Up: Regexp
Enter node , (file) or (file)node

How to Use Regular Expressions
==============================

   A regular expression can be used as a pattern by enclosing it in
slashes.  Then the regular expression is tested against the entire text
of each record.  (Normally, it only needs to match some part of the
text in order to succeed.)  For example, the following prints the
second field of each record that contains the string `foo' anywhere in
it:

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

   Regular expressions can also be used in matching expressions.  These
expressions allow you to specify the string to match against; it need
not be the entire current input record.  The two operators `~' and `!~'
perform regular expression comparisons.  Expressions using these
operators can be used as patterns, or in `if', `while', `for', and `do'
statements.  (Note: Control Statements in Actions.)  For
example:

     EXP ~ /REGEXP/

is true if the expression EXP (taken as a string) matches REGEXP.  The
following example matches, or selects, all input records with the
uppercase letter `J' somewhere in the first field:

     $ awk '$1 ~ /J/' inventory-shipped
     -| Jan  13  25  15 115
     -| Jun  31  42  75 492
     -| Jul  24  34  67 436
     -| Jan  21  36  64 620

   So does this:

     awk '{ if ($1 ~ /J/) print }' inventory-shipped

   This next example is true if the expression EXP (taken as a
character string) does _not_ match REGEXP:

     EXP !~ /REGEXP/

   The following example matches, or selects, all input records whose
first field _does not_ contain the uppercase letter `J':

     $ awk '$1 !~ /J/' inventory-shipped
     -| Feb  15  32  24 226
     -| Mar  15  24  34 228
     -| Apr  31  52  63 420
     -| May  16  34  29 208
     ...

   When a regexp is enclosed in slashes, such as `/foo/', we call it a
"regexp constant", much like `5.27' is a numeric constant and `"foo"'
is a string constant.


automatically generated by info2www version 1.2.2.9