打印列并计算字符串

时间:2012-06-06 20:06:31

标签: perl awk

我有一个大的列式文本文件,空格为demlimited

Name  subject       Result
John  maths         pass
John  science       fail
John  history       pass
John  geography     pass
Jack  maths         pass
jack  history       fail
kelly science       pass
kelly history       pass

我想计算每个名字(它是长名单,每个名字只应出现一次),其中有多少名通过。例如。对于约翰来说,他通过了3并且同样地为杰克通过了1.它应该将结果打印为

Name  Passcount
John   3
Jack   1
Kelly  2

任何人都可以使用awk或perl脚本。提前致谢

1 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情 -

awk '
BEGIN{ print "Name\tPasscount"} 
NR>1{if ($3=="pass") a[$1]++}
END{ for (x in a) print x"\t"a[x]}' file

测试:

$ cat file
Name  subject       Result
John  maths         pass
John  science       fail
John  history       pass
John  geography     pass
Jack  maths         pass
jack  history       fail
kelly science       pass
kelly history       pass

$ awk 'BEGIN{ print "Name\tPasscount"} NR>1{if ($3=="pass") a[$1]++}END{ for (x in a) print x"\t"a[x]}' file
Name    Passcount
Jack    1
kelly   2
John    3