MPE/iX Shell and Utilities Reference Manual, Vol 1

awk(1) MPE/iX Shell and Utilities awk(1)
The following program can be used to determine the number of lines in each input file:
{
++a[FILENAME]
}
END {
for (file in a)
if (a[file] = = 1)
print file, "has 1 line"
else
print file, "has", a[file], "lines"
}
The following program illustrates how you can use a two dimensional array in awk. Assume
the first field of each input record contains a product number, the second field contains a
month number, and the third field contains a quantity (bought, sold, or whatever). The pro-
gram generates a table of products versus month.
BEGIN {NUMPROD = 5}
{
array[$1,$2] += $3
}
END {
print "\t Jan\t Feb\tMarch\tApril\t May\t" \
"June\tJuly\t Aug\tSept\t Oct\t Nov\t Dec"
for (prod = 1; prod <= NUMPROD; prod++) {
printf "%-7s", "prod#" prod
for (month = 1; month <= 12; month++){
printf "\t%5d", array[prod,month]
}
printf "\n"
}
}
1-34 Commands and Utilities