exsonic01
exsonic01

Reputation: 637

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

Upvotes: 1

Views: 2281

Answers (2)

RARE Kpop Manifesto
RARE Kpop Manifesto

Reputation: 2805

The same approach without needing an extra counter variable :

awk '{ +$4 <= 12 || FNR -= $1 == "BB" } END { print 20 * (NR - FNR) } '

Upvotes: 0

Roman
Roman

Reputation: 6646

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

Upvotes: 3

Related Questions