如何使用Shell脚本放置数据透视表

问题描述 投票:-1回答:1

我有一个CSV文件中的数据,如下所示...

Emailid  Storeid      

[email protected] 2000

[email protected] 2001

[email protected] 2000

[email protected] 2000

[email protected] 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脚本,我没有得到想要的输出,你们能帮帮我吗?

shell csv pivot-table
1个回答
1
投票

使用miller (https:/github.comjohnkerlmiller。),然后从这个开始(我使用了一个CSV,因为我不知道你是用制表符还是用空格作为分隔符)

Emailid,Storeid
[email protected],2000
[email protected],2001
[email protected],2000
[email protected],2000
[email protected],2001

和运行

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

您将拥有

+---------+------------+
| Storeid | Emailcount |
+---------+------------+
| 2000    | 3          |
| 2001    | 2          |
+---------+------------+
© www.soinside.com 2019 - 2024. All rights reserved.