AWK matching problem

hi

i have a list as follow:

where "aba" can be zero or one. I want match all "aba" and count how many are set to one. I mean in the previous case i should get just "2":

I tried as follow:

awk '/^aba/ BEGIN{i=0}{if($2==1) i=i+1}END{print i}' file

but i get some errors.
If i do:

awk '/^aba/ {print $0}' do

I get the list with all "aba " i think that the problem refers to the BEGIN. Can anyone suggest a solution?

Thanks

D.

awk '$1 ~ /^aba/  && $2 ~ /1/ {i = i+1} END {print i}' a.txt

or

awk '$1 == "aba"  && $2 == "1" {i = i+1} END {print i}' a.txt

to be more precise in case you have things like "abalone 1" , use equality

awk '$1=="aba" && $2==1{c++}END{print c}' file

great!
thanks for both solutions

D.

EDIT: i found out also that in this way works:

awk '/^aba/ {if($2==1) i=i+1}END{print i}' file

:slight_smile:

awk '$0=="aba 1"{i++}END{print i}' file