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