简体   繁体   English

如何使用awk计算具有特定条件的列中数据的数量?

[英]How to count the number of data with specific conditions from columns using awk?

I have a data file of following format 我有以下格式的数据文件

AA 21.1218 14.7862 0.0566269
BB 26.5036 14.5513 19.975
CC 7.82448 1.30605 50.126899
AA 10.0179 4.3786 21.232036
BB 4.80236 4.23255 36.217038
CC 31.475 9.60365 7.237505
AA 8.39392 5.89571 10.30242
......

Here, I want to do is 在这里,我想做的是

check the 1st & 4th column, count the number of data which have the number greater than 12 in 4th column, and 'BB' in the first column, (in above example, it should be 2), then multiply 20 to that number. 检查第1列和第4列,在第4列中计数大于12的数据数,并在第一列中计数“ BB”(在上面的示例中应为2),然后将20乘以该数。 (Final answer should be 40) (最终答案应为40)

I was beginning from 我是从

awk '{print ($4>12)}' file > file2
wc -l file2

to check the 4th column's number, and print the line number. 检查第四列的编号,并打印行号。

But obviously they didn't worked. 但是显然他们没有工作。 It seems awk cannot use < or > operator inside. 看来awk不能在其中使用<或>运算符。 (greater than or less than) How can I achieve this oeration with cat, awk, sed, or grep? (大于或小于)如何使用cat,awk,sed或grep实现此操作?

Thanks 谢谢

To print matching lines: 要打印匹配的行:

awk '$1 == "BB" && $4 > 12' file

(the implicit action {print} is performed) (执行隐式操作{print}

You can also do the counting and the multiplication: 您还可以进行计数和乘法:

awk 'BEGIN {count = 0} $1 == "BB" && $4 > 12 {count++} END {print(count * 20)}' file

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

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