bash 对 csv 文件的第二列求和的命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18683368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:30:14  来源:igfitidea点击:

Command to sum 2nd colum of csv file

bashawk

提问by Ujjawal Khare

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

嗨,我有一个 csv 文件,其中有 2 列,第一列有名称,第二列有值。我想要的只是一个脚本,它可以将第二列的值相加,并将 csv 最后一行的输出打印为 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.

我使用的命令是在最后一行打印,但总计为 0。

$ awk '{print;s+=}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

回答by dngot_

$ awk -F"," '{print;x+=}END{print "Total " x}' ./test.csv 
CNG ,1
WND ,2
HUM ,1
AINGO   ,1
Total 5