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

BEGIN and END

BEGIN and END are two special patterns that give you a way to control initialization and wrap-up in an awk program. BEGIN matches before the first input record is read, so any statements in the action part of a BEGIN are done once, before the awk command starts to read its first input record. The pattern END matches the end of the input, after the last record has been processed.

The following awk program uses BEGIN to set the field separator to tab (\t) and to put column headings on the output. The field separator is stored in a built-in variable called FS. Although FS can be reset at any time, usually the only sensible place is in a BEGIN section, before any input has been read. The second printf statement of the program which is executed for each input line, formats the output into a table, neatly aligned under the column headings. The END action prints the totals. (Notice that a long line can be continued after a comma.)

   BEGIN { FS = "\t"
           printf "%10s %6s %5s   %s\n",
                  "COUNTRY", "AREA", "POP", "CONTINENT" }
         { printf "%10s %6d %5d   %s\n", $1, $2, $3, $4
           area = area + $2; pop = pop + $3 }
   END   { printf "\n%10s %6d %5d\n", "TOTAL", area, pop }
With the file countries as input, this program produces
      COUNTRY   AREA   POP   CONTINENT
         USSR   8650   262   Asia
       Canada   3852    24   North America
        China   3692   866   Asia
          USA   3615   219   North America
       Brazil   3286   116   South America
    Australia   2968    14   Australia
        India   1269   637   Asia
    Argentina   1072    26   South America
        Sudan    968    19   Africa
      Algeria    920    18   Africa
   

TOTAL 30292 2201


Next topic: Relational expressions
Previous topic: Patterns

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005