首页 > 解决方案 > 如何使用 Shell 脚本放置数据透视表

问题描述

我在 CSV 文件中有如下数据...

Emailid  Storeid      

a@gmail.com 2000

b@gmail.com 2001

c@gmail.com 2000

d@gmail.com 2000

e@gmail.com 2001

我期待下面的输出,基本上找出每个商店有多少个电子邮件 ID。

StoreID    Emailcount

2000           3

2001           2

到目前为止,我试图解决我的问题

IFS=","
while read f1 f2
do
awk -F, '{ A[$1]+=$2 } END { OFS=","; for (x in A) print x,A[x]; }' > /home/ec2-user/storewiseemials.csv
done < temp4.csv

使用上面的shell脚本我没有得到想要的输出,你们能帮帮我吗?

标签: shellcsvpivot-table

解决方案


使用 miller ( https://github.com/johnkerl/miller ) 并从这里开始(我使用了 CSV,因为我不知道您是使用制表符还是空格作为分隔符)

Emailid,Storeid
a@gmail.com,2000
b@gmail.com,2001
c@gmail.com,2000
d@gmail.com,2000
e@gmail.com,2001

并运行

mlr --csv count-distinct -f Storeid -o Emailcount input >output

你将会有

+---------+------------+
| Storeid | Emailcount |
+---------+------------+
| 2000    | 3          |
| 2001    | 2          |
+---------+------------+

推荐阅读