解析csv文件并通过特定公式计算相同数据

时间:2019-04-02 03:32:15

标签: linux bash csv

我的CSV文件带有:

block_num,date_time,miner_wallet,rewards,finished,verified
133,2019-02,0x212eccf2cccdecb1880e058ec40b21dd36f47c85,0.0,7,6
134,2019-02,0x212eccf2cccdecb1880e058ec40b21dd36f47c85,0.0,2,2
135,2019-02,0xd1dacf164f4bed77c37a26a79e9d63606cc35c82,0.0,7,6
135,2019-02,0x212eccf2cccdecb1880e058ec40b21dd36f47c85,0.0,7,7
136,2019-02,0x212eccf2cccdecb1880e058ec40b21dd36f47c85,0.0,3,3
136,2019-02,0xd1dacf164f4bed77c37a26a79e9d63606cc35c82,0.0,4,4
137,2019-02,0xa57d8b10d0186abf946f9b6a2709c680970fdd39,0.0,1,1

我需要使用矿工地址和最后一个字段“ verified”的总和乘以4,将每个相同的“ miner_wallet”计算为一个输出,因此输出为:

0x212eccf2cccdecb1880e058ec40b21dd36f47c85 72
0xd1dacf164f4bed77c37a26a79e9d63606cc35c82 40
0xa57d8b10d0186abf946f9b6a2709c680970fdd39 4

使用bash,awk或任何简单方法...

2 个答案:

答案 0 :(得分:0)

使用awk很容易。假设文件的第一行是标题行,则表明只需要跳过即可,您可以执行以下操作:

$ awk -F, '{ if (NR == 1) next; w[$3] += $6 * 4} 
    END { for (i in w) print i, w[i]}' file
0x212eccf2cccdecb1880e058ec40b21dd36f47c85 72
0xa57d8b10d0186abf946f9b6a2709c680970fdd39 4
0xd1dacf164f4bed77c37a26a79e9d63606cc35c82 40

说明

  • awk -F,将字段分隔符设置为','
  • '{ if (NR == 1) next;(如果行号为1,则跳至下一行以跳过标题,
  • w[$3] += $6 * 4}设置关联数组w[miner_wallet] = verified * 4
  • END { for (i in w) print i, w[i]}'输出数组索引和总值。

答案 1 :(得分:0)

awk 'BEGIN{FS=","} NR>1 {map[$3]+=$6;} END{for (key in map) {print key " " map[key] * 4;}}' i.csv
0x212eccf2cccdecb1880e058ec40b21dd36f47c85 72
0xa57d8b10d0186abf946f9b6a2709c680970fdd39 4
0xd1dacf164f4bed77c37a26a79e9d63606cc35c82 40

其中FS是字段分隔符,map是用于保存每个钱包总和的哈希图,而NR>1跳过带有标题的第一行。