Iacopo Olivo
Iacopo Olivo

Reputation: 1

AWK, can't match regex

I am learnig AWK, and I am trying to match any word from a file which contain at leas two vowels. My code is

#!/usr/bin/awk -f
BEGIN{c=0}
{   for(i=1; i<=NF; i++){
        if( match($i,"(.*[aeiou].*){2,}") > 0 )
            c++
    }
}
END{print c}

I am actually using, as test file, some random stuff, like

asd
e
ef
eseg <seg s<gko<
<sg<se
 eg eg eg 
PEAORJ<ÈMOFEPRIAGÒAJPD<SKAKROPDSAF
foeipsfipiè<+oèipiau
aeiouaeiou

I also tested my regex on myregextester.com and there it seems to work. what am I mistaking?

Upvotes: 0

Views: 93

Answers (1)

anubhava
anubhava

Reputation: 784898

Your awk script can be refactored (or simplified) to this (gnu-awk):

awk -v RS='[[:space:]]+' '/(.*[aeiouAEIOU].*){2}/{c++} END{print c+0}' file

It will output:

4

because to count words with vowels I have also included upper case vowels into character class.

-v RS='[[:space:]]+' will split input into records of single word.

Upvotes: 2

Related Questions