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

Using simple patterns

You can select specific records for printing or other processing by using simple patterns. awk has three kinds of patterns. First, you can use patterns called relational operators that make comparisons. As an example, the operator == tests for equality. Thus, to print the lines in which the fourth field equals the string ``Asia'', use the program consisting of the following single pattern:

   $4 == "Asia"
With the file countries as input, this program yields:
   CIS          8650	262	Asia
   China        3692	866	Asia
   India        1269	637	Asia
The complete set of comparisons are >, >=, <, <=, == (equal to), != (not equal to), ~ (matches), and ~! (does not match). These comparisons can be used to test both numbers and strings. For example, suppose you want to print only countries with a population greater than 100 million. All you need is the following program:
   $3 > 100
(Remember that the third field in the file countries is the population in millions.) This program prints all lines in which the third field exceeds 100.

Second, you can use regular expressions that search for specified characters to select records. The simplest form of a regular expression is a string of characters enclosed in slashes, as follows:

   /US/
This program prints each line that contains the (adjacent) letters US anywhere; with the file countries as input, it prints the following:
   USA	3615	219	North America
Third, you can use two special patterns, BEGIN and END, that match before the first record is read and after the last record is processed. The following program uses BEGIN to print a title:
   BEGIN   { print "Countries of Asia:\n" }
   /Asia/  { print "     ", $1 }
The output from this program is as follows:
   Countries of Asia:
         CIS
         China
         India

Next topic: BEGIN and END
Previous topic: Patterns

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003