使用 awk 基于两个匹配字段的求和列

发布于 2024-11-28 10:05:11 字数 361 浏览 1 评论 0原文

我似乎无法找到一个 awk 解决方案来完成这个简单的任务。我可以轻松地根据一个匹配字段 ($1) 对一列 ($3) 求和:

awk -F, '{array[$1]+=$3} END { for (i in array) {print i"," array[i]}}' datas.csv

现在,我如何根据两个字段执行此操作?比如说 1 美元和 2 美元?这是一个示例数据:

P1,gram,10  
P1,tree,12  
P1,gram,34  
P2,gram,23  
...

如果第一个和第二个字段匹配,我只需要对第 3 列求和。

感谢您的帮助!

I can't seem find an awk solution for this simple task. I can easily sum a column ($3) based on one matching field ($1) with :

awk -F, '{array[$1]+=$3} END { for (i in array) {print i"," array[i]}}' datas.csv

Now, how can I do that based on two fields ? Lets say $1 and $2 ? Here is a sample datas :

P1,gram,10  
P1,tree,12  
P1,gram,34  
P2,gram,23  
...

I simply need to sum column 3 if first and second fields match.

Thanx for any help !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-12-05 10:05:11

像这样

awk -F, '{array[$1","$2]+=$3} END { for (i in array) {print i"," array[i]}}' datas.csv

我的结果

P1,tree,12
P1,gram,44
P2,gram,23

编辑

由于OP需要逗号保留在输出中,我使用@yi_H的“逗号修复”编辑了上面的答案。

Like so

awk -F, '{array[$1","$2]+=$3} END { for (i in array) {print i"," array[i]}}' datas.csv

My result

P1,tree,12
P1,gram,44
P2,gram,23

EDIT

As the OP needs the commas to remain in the output, I edited the answer above using @yi_H's "comma fix".

迷鸟归林 2024-12-05 10:05:11

对于需要较少内存但需要首先排序的解决方案(没有什么是免费的):

sort datas.csv | awk -F "," 'NR==1{last=$1 "," $2; sum=0;}{if (last != $1 "," $2) {print last "," sum; last=$1 "," $2; sum=0;} sum += $3;}END{print last "," sum;}'

For a solution needing less memory, but needing sorting first (nothing is free):

sort datas.csv | awk -F "," 'NR==1{last=$1 "," $2; sum=0;}{if (last != $1 "," $2) {print last "," sum; last=$1 "," $2; sum=0;} sum += $3;}END{print last "," sum;}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文