|
|
BEGIN and END are two special patterns that give you a way of controlling 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 END pattern 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 program's second printf statement, 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 continue 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 the following output:
COUNTRY AREA POP CONTINENT CIS 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 AfricaTOTAL 30292 2201