Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command to sum 2nd colum of csv file

Tags:

bash

awk

hi i have a csv file which has 2 coloumns first column has names and secons has values. All i want is a script that can sum values of second column and print output in last row of csv as Total

example of file:-

CNG 2128485188
WND 222047363
HUM 283010928
AINGO   253694944

The command i am using is printing in last line but giving total as 0.

$ awk '{print;s+=$2}END{printf "Total %'\''d\n",s}' /cygdrive/c/KPI/test/SCCP_ADMIN_RAW2.csv | tail -10
LIMIT,27789
VDEOT,21109
CELZA,627
DUUNI,26636
EMBLT,1255927
URA,521
MONTE,1789
EGLMO,391
DGTEL,394
Total 0
like image 661
Ujjawal Khare Avatar asked Sep 08 '13 11:09

Ujjawal Khare


People also ask

How do you sum a column in Linux?

“UtilityBills. txt” represents the name of the text file from which we have to read the data. Then we have the “awk” keyword followed by the “sum” expression that will actually calculate the sum from the second column of our dataset, and then the “print” command will be used to display the results on the terminal.

How many columns can CSV handle?

csv files have a limit of 32,767 characters per cell. Excel has a limit of 1,048,576 rows and 16,384 columns per sheet. CSV files can hold many more rows. You can read more about these limits and others from this Microsoft support article here.


1 Answers

$ awk -F"," '{print;x+=$2}END{print "Total " x}' ./test.csv 
CNG ,1
WND ,2
HUM ,1
AINGO   ,1
Total 5
like image 69
dngot_ Avatar answered Sep 22 '22 03:09

dngot_