moadeep
moadeep

Reputation: 4098

sum column based on flag

I have a file with a format similar to (in reality there are many more columns and rows)

ID flag
1   a
2   n
3   n
4   a
5   n
6   n
7   a
8   a
9   n
10  n
11  n
12  n

I want to find out how many rows have the value n in $2.

I have tried awk '{if ($2 == 'n') SUM += 1} END {print SUM}' filename

This does not work. Any ideas?

Upvotes: 1

Views: 198

Answers (1)

Guru
Guru

Reputation: 16994

One way:

awk '$2=="n"{x++}END{print x}' file

Upvotes: 3

Related Questions