Bash - 如何组合重复的行并对csv文件的值求和?

时间:2018-06-07 08:28:29

标签: bash

我有这个csv文件:

Apple,5
Apple,2
Banana,3
Banana,7
Avocado,1
Grapefruit,9

我希望将左侧的重复行组合在一起并对右侧的值求和。这是我想要的最终结果:

Apple,7
Banana,10
Avocado,1
Grapefruit,9

我知道这更多是Excel的内容,但我想学习如何使用bash脚本。到目前为止,我只能这样排序:

sort -u file.csv

我不知道如何根据左边的每个水果对右边的值求和。我该怎么做?

编辑:下面提供的答案不适用于我的实际文件,所以这里是完整的csv文件

Order Date,Username,Order Number,No Resi,Quantity,Title,Update Date,Status,Price Per Item,Status Tracking,Alamat
05 Jun 2018,Mildred@email.com,205583995140400,,2,Gold,05 Jun 2018 – 10:01,In Process,Rp3.000.000,Done,Syahrul Address
05 Jun 2018,Mildred@email.com,205583995140400,,1,Gold,05 Jun 2018 – 10:01,In Process,Rp3.000.000,Done,Syahrul Address
05 Jun 2018,Martha@email.com,205486016644400,,2,Gold,05 Jun 2018 – 10:01,In Process,Rp3.000.000,Done,Faishal  Address
05 Jun 2018,Martha@email.com,205486016644400,,2,Gold,05 Jun 2018 – 10:01,In Process,Rp3.000.000,Done,Faishal  Address
05 Jun 2018,Misty@email.com,205588935534900,,2,Gold,05 Jun 2018 – 10:01,In Process,Rp3.000.000,Done,Rutwan Address
05 Jun 2018,Misty@email.com,205588935534900,,1,Gold,05 Jun 2018 – 10:01,In Process,Rp3.000.000,Done,Rutwan Address

我希望最终结果如下:

Order Date,Username,Order Number,No Resi,Quantity,Title,Update Date,Status,Price Per Item,Status Tracking,Alamat
05 Jun 2018,Mildred@email.com,205583995140400,,3,Gold,05 Jun 2018 – 10:01,In Process,Rp3.000.000,Done,Syahrul Address
05 Jun 2018,Martha@email.com,205486016644400,,4,Gold,05 Jun 2018 – 10:01,In Process,Rp3.000.000,Done,Faishal  Address
05 Jun 2018,Misty@email.com,205588935534900,,3,Gold,05 Jun 2018 – 10:01,In Process,Rp3.000.000,Done,Rutwan Address

我只想将Quantity行加在一起,而剩下的就是原样。我如何用awk做到这一点?

0 个答案:

没有答案