shell - 如何使用 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脚本我没有得到想要的输出,你们能帮帮我吗?
解决方案
使用 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 |
+---------+------------+
推荐阅读
- testing - 如何模拟网络负载以测试两台服务器之间的连接性?
- sql-server - 如何根据格林威治标准时间计算当地时间?
- javascript - Chrome 扩展,选项页面,显示黑色矩形而不是列表
- c# - 让 Unity 显示 StyleCop 错误
- r - Can I pass a textInput to a Regex search
- mysql - 当内部连接值可能为空时,如何填充表?
- javascript - JS 使用与对象键相同的引号解析包裹在字符串中的对象
- javascript - 迭代对象
- loopback - 为什么环回接口上的 tcpdump 只捕获过滤器接收到的一半数据包?
- python-3.x - 抽象模型中的 Pyomo 约束块