Introducing UNIX and Linux |
AwkOverview |
Accessing ValuesIn 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
( # 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 += $2*$3 Thus, Analogous to Worked exampleCalculate the average price per kilo of the vegetables you have
purchased. # 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