简体   繁体   English

如何在Linux中对大于值的行进行计数并将计数写入文本文件

[英]How to count rows greater than a value and write the count to a text file in Linux

I have a file with sample data shown below: 我有一个带有示例数据的文件,如下所示:

%_above_10 %_above_20 %_above_30 %_above_50
88.6    88.1    87.8    87.2
89.1    78.5    72.3    59.4
100.0   100.0   100.0   100.0
100.0   100.0   100.0   100.0
100.0   100.0   70.0   80.0
100.0   100.0   100.0   80.0
100.0    31.9    26.8    17.4
00.0   96.0    77.3    43.3
68.8    65.9    63.6    57.1

I need to count the rows in each column which have value 100 and write it to a text file. 我需要计算每列的值为100的行并将其写入文本文件。 The output could look like: 输出看起来像:

Totalrows %_above_10 %_above_20 %_above_30 %_above_50
   9        5            4          3         2

I am able to count one column at a time using the basic awk command: 我可以使用基本的awk命令一次计算一列:

awk -F "\t" '{if($1 == 100)print;}

Could anyone suggest a way to count the columns in the above specified way and write to a file using linux commands or script (awk). 任何人都可以提出一种以上述指定的方式对列进行计数并使用linux命令或脚本(awk)写入文件的方法。 Thanks 谢谢

A way with awk and column. 一种用awk和column的方式。

awk 'NR==1{print "TotalRows "$0;next}
     {for(i=1;i<=NF;i++)a[i]+=$i==100}
     END{print NR-1,a[1],a[2],a[3],a[4]}' file | column -t

More general 更一般

 awk 'NR==1{print "TotalRows "$0;next} 
      {a[0]++;for(i=1;i<=NF;i++)a[i]+=$i==100}
      END{for(i=0;i<=NF;i++)printf "%s ",a[i];print t}' test | column -t

Output 输出量

TotalRows  %_above_10  %_above_20  %_above_30  %_above_50
9          5           4           3           2

Awk solution: AWK解决方案:

$ cat chas.awk
NR == 1 { hdr = $0; next }    
{
    a[1] += ($1 == 100) ? 1 : 0;
    a[2] += ($2 == 100) ? 1 : 0;
    a[3] += ($3 == 100) ? 1 : 0;
    a[4] += ($4 == 100) ? 1 : 0;
}
END { print "Totalrows", hdr; print (NR-1),a[1],a[2],a[3],a[4] }

$ awk -f chas.awk chas.txt
Totalrows %_above_10 %_above_20 %_above_30 %_above_50
9 5 4 3 2

Or a more general solution with respect to the number of columns: 或更常见的列数解决方案:

NR == 1 { hdr = $0; cols = NF; next }

{
    for (i=1; i<=NF; ++i) {
      a[i] += ($i == 100) ? 1 : 0;
    }
}

END {
    print "Totalrows", hdr
    printf (NR-1)
    for (i=1; i<=cols; ++i) {
      printf " " a[i]
    }
    print "" # add final newline
}

Perl solution: Perl解决方案:

perl -lane '$F[$_] == 100 and $c[$_]++ for 0 .. $#F }{ print $.-1, " @c"' < input
  • -l adds a newline to print -l添加换行符以print
  • -a splits the input to the @F array -a将输入拆分为@F数组
  • for each line, the script goes over the records and adds 1 to the particular counter if it sees 100 对于每一行,脚本将遍历记录,如果看到100,则将1加到特定计数器
  • once the file ends, the counter is printed, preceded by the number of files read minus one (you don't count the header) 文件结束后,将打印计数器,其后是读取的文件数减一(您不计算标题)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM