Main index

Introducing UNIX and Linux


Awk

Overview
What is 'awk'?
Invoking 'awk'
Naming the fields
Formatted output
      Operators used by Awk
Patterns
Variables
      Accessing Values
      Special variables
Arguments to 'awk' scripts
Arrays
Field and record separators
Functions
      List of Awk functions
Summary
Exercises

Accessing Values

In the shell, we use variables, which we name, and access their values by placing a dollar before their names. In Awk we also have variables, to which we can assign values in the same way as the shell, but to use a variable we do not need the dollar. The reason the shell needs the dollar is a technical one, relating to ensuring that each shell statement is unambiguous. The ambiguities that might arise in the shell do not occur in Awk.

Suppose we require a total grocery bill. We could use a variable (total, say) to keep a 'running total' of the cost of each vegetable and print it out at the end:

# Initialise total to 0
BEGIN { total = 0 }

# For each line of input, that is, each vegetable, add
# the total cost to variable total
{ total = total + $2*$3 }

# At the end, print out the total
END { printf "Total cost is %.2f\n", total}

Some explanation is required for the action total = total + $2*$3: the current value stored in the variable total has the values of $2*$3 added to it, and the resulting number is stored back in total. So the value of the variable total has been updated. Another way of stating this action is to use the symbol +=, so:

total += $2*$3

Thus, total += ... is just shorthand for total = total + ...

Analogous to += are -=, *=, /=, %= and ^=.

Worked example

Calculate the average price per kilo of the vegetables you have purchased.
Solution: We need to total the amount of money spent, and also the number of kilograms. The final answer is calculated by dividing one by the other.

# Use variable totalcost for the money spent
# Use variable totalweight for the total kilos
# Initialise totalcost and totalweight to 0
BEGIN { totalcost = 0
        totalweight = 0 }

# For each line of input update the running totals
{ totalcost = totalcost + $2*$3 }   # Cost
{ totalweight = totalweight + $3 }  # Weight

# At the end, print out the average
END { printf "Average cost is %.2f pounds per kilo\n", totalcost/totalweight}

Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck