简体   繁体   中英

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. (Final answer should be 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. (greater than or less than) How can I achieve this oeration with cat, awk, sed, or grep?

Thanks

To print matching lines:

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

(the implicit action {print} is performed)

You can also do the counting and the multiplication:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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