Sunday, December 12, 2010

Awk - Sum of multiple columns of file


Input file:

$ cat file.txt
500:120:100
100:120:700
200:900:125
120:120:900

Required:
Compute the sum of individual columns of the above file. i.e. required output:

920:1260:1825

Awk solution - 1:

$ awk 'BEGIN {FS=OFS=":"}
NR == 1 { n1 = $1; n2 = $2; n3 = $3; next }
{ n1 += $1; n2 += $2; n3 += $3 }
END { print n1, n2, n3 }' file.txt

Output:

920:1260:1825

Awk solution - 2:

$ awk -F ":" '
{ for (i=1; i<=NF; ++i) sum[i] += $i; j=NF }
END { for (i=1; i <= j; ++i) printf "%s ", sum[i]; printf "\n"; }
' file.txt

Output:

920 1260 1825

And the solution for finding sum of numbers in each row of a file (i.e. horizontal sum) is here

2 comments:

Mike M. said...

I appreciate your posts on this and a similar post on 3/24/2010.

Here is my scenario, I have two columns. I need to add the first columns (the easy part). The second column is a percent that I need to subtract by 100 then multiply against $1 for each line, then sum them

Here is the input:

942330863 96
942172150 95
942099452 92

I need this as an output:

2826602465 160169797

I tried this code, but it's just doing the percentage work and only doing it for the first line and not summing every line.

awk '
BEGIN {FS=","}
NR == 1{ n1 = $1; x = substr((100-$2)/100,2,3); y = x*$2; next }
{ n1 += $1; y += $y }
END { printf ("%-15d%d\n",n1,y)
}'

Jadu Saikia said...

@Mike M:

I think this is the problem:

y += $y

should be

y += y


?

© Jadu Saikia www.UNIXCL.com