简体   繁体   English

使用awk计数具有不同条件的行

[英]count lines with different conditions using awk

I need to get the number of lines with 2 different conditions for 1 text file. 我需要获取1个文本文件的2种不同条件的行数。 The first condition is that values of the third column are smaller than 10 so I can do it by the following script: 第一个条件是第三列的值小于10,因此我可以通过以下脚本进行操作:

awk '$3<=10' DATA_File | wc -l

The second condition is just to get a total number of lines in the same file this I can get by: 第二个条件是在我可以通过的同一文件中获取行总数:

awk 'END { print FNR}' DATA_File

or 要么

awk '$3' DATA_File | wc -l

However, what I don't know is how to merge these to commands in a single string so I can get the result saved in a separate file with one string separated by either "tab" or "space" consisting of "number of string with <10", "total number of strings", "their ratio/ or percentage" 但是,我不知道如何将它们合并到单个字符串中的命令中,这样我就可以将结果保存在一个单独的文件中,其中一个字符串由“ tab”或“ space”分隔,其中“ tab”或“ space”由“ <10”,“字符串总数”,“其比率/或百分比”

for instance the file is: 例如文件是:

wer fre 11
grt o34 5
45f 123 45

the output I need is: 我需要的输出是:

2 3 0.66/ or 66%

I could write a small script on python which would do it but due to a number of reasons bash would be much more convenient. 我可以在python上编写一个小型脚本来执行此操作,但是由于多种原因,bash会更加方便。

You can for example say: 例如,您可以说:

$ awk '$3<=10 {min10++} END {print min10, FNR, (FNR?min10/FNR:0)}' file
1 3 0.333333

Or print and output to a file like print ... > "new_file" . 或者print并输出到诸如print ... > "new_file"类的文件。

You can also use printf to provide a better format: 您还可以使用printf提供更好的格式:

$ awk '$3<=10 {min10++} END {printf "%d %d %.2f%\n", min10, FNR, (FNR?min10/FNR:0)}' file
1 3 0.33%

The (FNR?min10/FNR:0) trick is courtesy of Ed Morton and is used to prevent diving by zero. (FNR?min10/FNR:0)技巧是由Ed Morton提供的,用于防止零跳水。

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

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