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

gawk: Two Rules

 
 1.4 An Example with Two Rules
 =============================
 
 The 'awk' utility reads the input files one line at a time.  For each
 line, 'awk' tries the patterns of each rule.  If several patterns match,
 then several actions execute in the order in which they appear in the
 'awk' program.  If no patterns match, then no actions run.
 
    After processing all the rules that match the line (and perhaps there
 are none), 'awk' reads the next line.  (However, ⇒Next Statement
 and also ⇒Nextfile Statement.)  This continues until the program
 reaches the end of the file.  For example, the following 'awk' program
 contains two rules:
 
      /12/  { print $0 }
      /21/  { print $0 }
 
 The first rule has the string '12' as the pattern and 'print $0' as the
 action.  The second rule has the string '21' as the pattern and also has
 'print $0' as the action.  Each rule's action is enclosed in its own
 pair of braces.
 
    This program prints every line that contains the string '12' _or_ the
 string '21'.  If a line contains both strings, it is printed twice, once
 by each rule.
 
    This is what happens if we run this program on our two sample data
 files, 'mail-list' and 'inventory-shipped':
 
      $ awk '/12/ { print $0 }
      >      /21/ { print $0 }' mail-list inventory-shipped
      -| Anthony      555-3412     anthony.asserturo@hotmail.com   A
      -| Camilla      555-2912     camilla.infusarum@skynet.be     R
      -| Fabius       555-1234     fabius.undevicesimus@ucb.edu    F
      -| Jean-Paul    555-2127     jeanpaul.campanorum@nyu.edu     R
      -| Jean-Paul    555-2127     jeanpaul.campanorum@nyu.edu     R
      -| Jan  21  36  64 620
      -| Apr  21  70  74 514
 
 Note how the line beginning with 'Jean-Paul' in 'mail-list' was printed
 twice, once for each rule.
 
© manpagez.com 2000-2018
Individual documents may contain additional copyright information.