Date: May 21, 2005
The awk* command is a powerful tool for extracting and manipulating data from a file. The following examples illustrate different ways "awk" can be used to sum columns of numbers in a file.
# sum1.awk -
{ sum+= $1 }
END { print sum }
#filter_sum.awk
/AIX/ { sum+=$2}
END { print sum }
#sum2.awk
{ sum+=$COL}
END { print sum }
#sum_all.awk
{ for (i=1; i<=NF; i++) { sum[i]+= $i } }
END { for (i=1; i<=NF; i++ )
{ print "Col[", i, "] =", sum[i] }
}
The awk command has many more capabilities than just summing columns. Some of the ways I've used awk include
For more information search the web, and/or see the AIX URL http://publib16.boulder.ibm.com/doc_link/en_US/a_doc_lib/cmds/aixcmds1/awk.htm
* the awk command is named after its authors:
Aho Weinberger Kernighan
Bruce Spencer,
baspence@us.ibm.com
May 21, 2005