Awk:按报告创建组?
我有一个包含制表符分隔数据的 ASCII 文件,如下所示:
Test_Version=2.5.3 Model-Manufacturer=D12-500
Test_Version=2.5.3 Model-Manufacturer=D12-500
Test_Version=2.6.3 Model-Manufacturer=H23-600
Test_Version=2.6.3 Model-Manufacturer=HR21-100
Test_Version=2.6.3 Model-Manufacturer=HR21-100
Test_Version=2.6.4 Model-Manufacturer=R16-300
我想使用 Awk 创建一个报告,其中对按“Test_Version”分组的“模型制造商”进行计数:
Test_Version Model-Manufacturer Count-Model-Manufacturer
2.5.3 D12-500 2
2.6.3 HR21-100 2
2.6.3 H23-600 1
2.6.4 R16-300 1
这是我开始的内容,但它不起作用...
awk 'BEGIN {FS="\t";} {vercounts[$1]=vercounts[$1]+1;mdlcounts[$2]=mdlcounts[$2]+1}
END {for (key in vercounts)
printf "%s\t%d\n", key, vercounts[key];} {for (key in mdlcounts) printf "%s\t%d\n", key, mdlcounts[key];}
' data_file
是吗 ?可能的?你会怎样做呢?
I have an ASCII file that contains tab-delimited data like this:
Test_Version=2.5.3 Model-Manufacturer=D12-500
Test_Version=2.5.3 Model-Manufacturer=D12-500
Test_Version=2.6.3 Model-Manufacturer=H23-600
Test_Version=2.6.3 Model-Manufacturer=HR21-100
Test_Version=2.6.3 Model-Manufacturer=HR21-100
Test_Version=2.6.4 Model-Manufacturer=R16-300
I want to create with Awk a report that counts "Model-Manufacturer" grouped by "Test_Version":
Test_Version Model-Manufacturer Count-Model-Manufacturer
2.5.3 D12-500 2
2.6.3 HR21-100 2
2.6.3 H23-600 1
2.6.4 R16-300 1
This is what I started with but it's not working...
awk 'BEGIN {FS="\t";} {vercounts[$1]=vercounts[$1]+1;mdlcounts[$2]=mdlcounts[$2]+1}
END {for (key in vercounts)
printf "%s\t%d\n", key, vercounts[key];} {for (key in mdlcounts) printf "%s\t%d\n", key, mdlcounts[key];}
' data_file
Is it possible? How would you go about it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我看来,你真正想做的是计算重复行的数量。抛开漂亮的印刷,你真正需要的是:
或者我在这里误解了什么?
The way I see it, what you really want to do is to count the number of repeated lines. Leaving out the pretty printing, all you really need is:
Or am I misunderstanding something here?
我不确定我是否理解您的期望。但我想
会做的。
I am not sure if I understand what you are expecting. But I think
will do.
以下 awk 片段将准确给出您正在寻找的内容
Following piece of awk snippet will give exactly what you are looking for