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

Simple actions

We have already seen the simplest action of an awk program: printing each input line. Now let us consider how you can use built-in and user-defined variables and functions for other simple actions in a program.

Built-in variables

Besides reading the input and splitting it into fields, awk counts the number of records read and the number of fields within the current record; you can use these counts in your awk programs. The variable NR is the number of the current record, and NF is the number of fields in the record. So the program

   { print NR, NF }
prints the number of each line and how many fields it has, while
   { print NR, $0 }
prints each record preceded by its record number.

User-defined variables

Besides providing built-in variables like NF and NR, awk lets you define your own variables, which you can use for storing data, doing arithmetic, and the like. To illustrate, consider computing the total population and the average population represented by the data in the file countries:

   { sum = sum + $3 }
   END { print "Total population is", sum, "million"
         print "Average population of", NR, "countries is",
         sum/NR }


NOTE: awk initializes sum to zero before using it.

The first action accumulates the population from the third field; the second action, which is executed after the last input, prints the sum and average:

Total population is 2201 million
Average population of 10 countries is 220.1

Functions

Built-in functions of awk handle common arithmetic and string operations for you. For example, one of the arithmetic functions computes square roots; a string function substitutes one string for another. awk also lets you define your own functions. Functions are described in detail in ``Actions''.


Next topic: A handful of useful one-liners
Previous topic: Simple patterns

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