根据column1打印column2的数量

时间:2013-03-22 14:10:42

标签: awk

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

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

1 个答案:

答案 0 :(得分:0)

以下是适合您的代码:

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