Awk:按报告创建组?

发布于 2024-10-19 01:41:46 字数 991 浏览 1 评论 0原文

我有一个包含制表符分隔数据的 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 技术交流群。

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

发布评论

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

评论(4

十年九夏 2024-10-26 01:41:46

在我看来,你真正想做的是计算重复行的数量。抛开漂亮的印刷,你真正需要的是:

{ counts[$0]++; } 
END { for (key in counts) { print key, counts[key]; } }

或者我在这里误解了什么?

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:

{ counts[$0]++; } 
END { for (key in counts) { print key, counts[key]; } }

Or am I misunderstanding something here?

酒儿 2024-10-26 01:41:46

我不确定我是否理解您的期望。但我想

{a[$0]++}  
END {for (i in a)  { print i, a[i]}  
}

会做的。

I am not sure if I understand what you are expecting. But I think

{a[$0]++}  
END {for (i in a)  { print i, a[i]}  
}

will do.

Spring初心 2024-10-26 01:41:46
awk -F '\t' '
  {
    split($1, ver, /=/)
    split($2, model, /=/)
    count[ver[2] FS model[2]]++
  }
  END {for (key in count) print key FS count[key]}
'
awk -F '\t' '
  {
    split($1, ver, /=/)
    split($2, model, /=/)
    count[ver[2] FS model[2]]++
  }
  END {for (key in count) print key FS count[key]}
'
全部不再 2024-10-26 01:41:46

以下 awk 片段将准确给出您正在寻找的内容

awk -F" " '{ vercounts[$0]+=1} END { for ( i in vercounts ) { a=i; gsub("Test_Version=|Model-Manufacturer=","",a) ; print a,vercounts[i]} }' file_name
2.6.4  R16-300 1
2.6.3  HR21-100 2
2.6.3  H23-600 1
2.5.3  D12-500 2

Following piece of awk snippet will give exactly what you are looking for

awk -F" " '{ vercounts[$0]+=1} END { for ( i in vercounts ) { a=i; gsub("Test_Version=|Model-Manufacturer=","",a) ; print a,vercounts[i]} }' file_name
2.6.4  R16-300 1
2.6.3  HR21-100 2
2.6.3  H23-600 1
2.5.3  D12-500 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文