根据column1打印column2的数量(print the number of column2 based on column1)

编程入门 行业动态 更新时间:2024-10-26 23:38:27
根据column1打印column2的数量(print the number of column2 based on column1)

我有一个文件如下所示。 我想打印每个科目的学生人数(重复的名字不想计算)。

alvy science eby maths alvy science bitty science monika maths eby maths johny social

如何使用awk获得以下输出?

`no:of students in` science- 2 no:of students in maths - 2 no:of students in social - 1

I have a file as shown below. I would like to print the number of students in each subject(duplicated names don't want to count).

alvy science eby maths alvy science bitty science monika maths eby maths johny social

How do I get the following output with awk?

`no:of students in` science- 2 no:of students in maths - 2 no:of students in social - 1

最满意答案

以下代码应该适合您:

awk '!s[$1, $2]++ {arr[$2]++} END {for (i in arr) print "no:of students in", i, "-", arr[i]}'

Here is the code that should work for you:

awk '!s[$1, $2]++ {arr[$2]++} END {for (i in arr) print "no:of students in", i, "-", arr[i]}'

更多推荐

本文发布于:2023-07-30 01:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321370.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数量   based   number   print

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!