计算矩阵行中的唯一数字

时间:2016-06-22 18:50:08

标签: shell awk

我有一个矩阵:

   >ACTTT  ASB  0.098
    0   0      1    0   
 0.75   0   0.25    0   
    0   0      0    1   
    0   1      0    0   
    1   0      0    0   
    1   0      0    0   
    0   1      0    0   
    0   1      0    0   

我想算一下' 0' 0' 0的出现次数。'在每行文件中(忽略标题),这样每行我都会得到3,2,3,3,3,3,3的计数。 我试过echo $ line | grep -o 0 | wc -l,但它算的是' 0。'同样。

3 个答案:

答案 0 :(得分:2)

$ awk 'NR>1{print gsub(/(^|[[:space:]])0([[:space:]]|$)/,"&")}' file
3
2
3
3
3
3
3
3

答案 1 :(得分:0)

像下面这样的awk脚本可以工作:

<强> script.awk

BEGIN  { ORS = "," }
NR > 1 { s =0
         for( f=1; f<= NF; f++) s+= (($f == 0) ? 1 : 0 )
         print s
       }

像这样使用:awk -f script.awk yourfile

第一行将,配置为Output-Record-Separator,第二行应用于除标题之外的每一行。它遍历字段,如果字段$f等于0:?运算符用于递增s中的零行和。

答案 2 :(得分:0)

一件事拳头。看起来你正在shell循环中逐行处理文件。这很慢。你绝对应该使用awk并在运行中处理该文件。其他答案显示了如何做到这一点。

以下只是理论提示如何使用 while True: c = msvcrt.getch() if ord(c) == 8: buffer = buffer[:-1] else: buffer += c # write the buffer to file <div id="DivDati" class="ui-accordion-content ui-helper-reset ui-widget- content ui-corner-bottom"> <p><a style="margin:20px;" class="button" href="javascript:EsportaConfrontoCanali(false);">Esporta</a></p> <!-- Tabella --> <table style="width:800px;margin:auto;" class="confrontoTabelle"> <tbody><tr> <th>Selezionare i valori e premere "Filtra"</th> </tr> </tbody></table> <!-- Fine tabella --> </div> 来完成。我只关注正确的正则表达式。

你可以grep grep前缀为空格或行的开头,然后是空格或行尾:

wc

head命令是跳过标题行。

相关问题