Sum of nth column in a file using awk
Today I had to find sum of nth column entries from one of my csv files. I just googled and found a nice way of doing it using awk.
Suppose my input csv file is named as test.out and I have to find sum of all nth column entries of it.
I just created an awk script file named test.awk for setting the FS variable and providing the summation logic to awk.
#test.awk
BEGIN {FS=","}
{ sum+=$COL}
END { print sum }
Then every thing is easy
Use the awk command as follows with input file test.out. Replace 8 with any column index you want.
awk -f test.awk COL=8 < test.out
2 comments:
How it would work for floating point numbers? :-)
Sure!
You can use "bc" command to help u out for floating point numbers!!
Post a Comment