grep – Find Words with All Vowels in Order from a File

grepregular expression

I have a large dictionary file with 300,000+ words in it and I'm trying to find all words with the vowels aeiou in that order and have only exactly 5 vowels. My current attempt does not seem to be working and for the life of me I don't understand why.

less mywords | grep -iE [^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u[^aeiou]*

I think it get's all the words I'm looking for but there are a few words that pop up which I can not get rid of.

abstemiousnesses
ultraserious

There are a few other but they are in the same vein. Curiously, even if I add something like [^u]* to the front ultraserious keeping popping back up! Any solution would be fine however I would like it restricted to grep as we've been told it can be done using only grep.

Best Answer

You're not anchoring the expression. It can match in the middle, so any vowels "outside" your match are allowed.

Add a ^ and $ to prevent that.

$ echo abstemiousnesses | grep -iE '[^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u[^aeiou]*'
abstemiousnesses
$ echo abstemiousnesses | grep -iE '^[^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u[^aeiou]*$'
Related Question