DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with awk

Combinations of patterns

A compound pattern combines simpler patterns with parentheses and the logical operators || (or), && (and), and ! (not). For example, suppose you want to print all countries in Asia with a population of more than 500 million. The following program does this by selecting all lines in which the fourth field is Asia and the third field exceeds 500:

   $4 == "Asia" && $3 > 500
The program
   $4 == "Asia" || $4 == "Africa"
selects lines with Asia or Africa as the fourth field. Another way to write the latter query is to use an extended regular expression with the alternation operator | :
   $4 ~ /^(Asia|Africa)$/

The negation operator ! has the highest precedence, then &&, and finally ||. The operators && and || evaluate their operands from left to right; evaluation stops as soon as truth or falsehood is determined.


Next topic: Pattern ranges
Previous topic: awk extended regular expressions

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004