Iacopo Olivo February 2016

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?

Answers


anubhava February 2016

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.

Post Status

Asked in February 2016
Viewed 1,260 times
Voted 13
Answered 1 times

Search




Leave an answer