Posts Tagged ‘awk’

Using awk to sum all entries in one column

Had to search this one for a while… here is the source file:

360109368 15004557 java.lang.String
310090432 7692644 char[]
179389488 7474562 java.util.HashMap$Entry
71350968 70689 java.util.HashMap$Entry[]
14298360 109019 * ConstMethodKlass
7853616 109019 * MethodKlass
7409816 119136 java.lang.Object[]
7302080 17556 int[]
7094864 11288 byte[]
I wanted the sum of all the numbers in the first column and heres the awk line I used:
cat sourcefile | awk ‘{print $1}’ | awk ‘BEGIN{sum=0}{sum+=$1}END{print “Total: ” sum}’