我正在尝试获取一个包含许多1000行的文件,并且对于每行我想计算个别字段出现的次数。因此,该文件的几行示例如下所示:
0/0 0/0 0/0 0/0 0/0 0/0 0/1 0/0 0/0 1/1 0/0 0/1
0/1 1/1 0/1 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0
我的代码是:
cat file | awk 'BEGIN{a=0; c=0; g=0;} {a+=gsub("0/0",""); c+=gsub("0/1",""); g+=gsub("1/1","")} END{print a,c,g}'
我得到的输出是:
18 4 2
我想要的输出是:
line #1- 9 2 1
line #2- 9 2 1
我似乎无法弄清楚我做错了什么
答案 0 :(得分:1)
awk '{print "line #", NR, "-", gsub("0/0",""), gsub("0/1",""), gsub("1/1","")}' file
答案 1 :(得分:0)
使用GNU awk
你可以尝试这样的事情(输出可以是随机的。既然你没有提到你想要的订单,我假设这应该没问题):
awk '{delete a; printf "line #%s- ", NR; for(x=1;x<=NF;x++) a[$x]++; for(y in a) printf "%s ", a[y]; print ""}' file
$ cat file
0/0 0/0 0/0 0/0 0/0 0/0 0/1 0/0 0/0 1/1 0/0 0/1
0/1 1/1 0/1 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0 0/0
$ awk '{delete a; printf "line #%s- ", NR; for(x=1;x<=NF;x++) a[$x]++; for(y in a) printf "%s ", a[y]; print ""}' file
line #1- 9 2 1
line #2- 9 2 1