For example, i have file with content

hello world
it's nice to see you
amazing night
what a wonderful day
my name is Robert
still breathing
speaking bottom soul
something wrong

I need to match those lines, in which second word has exactly two vowels. So the output should be:

it's nice to see you
my name is Robert
speaking bottom soul

How can I do this using grep?

  • @thrig, i don't have much experience in working with regular expressions, so the last thing i tried grep -e ".*\s[aueioy]\{2\}" test.txt. BTW, it's still wrong. – Maksym Musiienko Dec 10 '16 at 17:07
  • Does it have to be grep? it would be simpler and clearer IMHO using a tool such as awk or perl that handles whitespace delimited fields more naturally. For example in awk you could use the return value of a gsub on $2 (or in perl use the return value of a tr on $F[1]). – steeldriver Dec 10 '16 at 18:34

grep with extended regular expressions:

grep -iE '^[^[:blank:]]+[[:blank:]]+([^aeiou]*[aeiou]){2}[^aeiou]*\>' file

grep with pcre

grep -iP '^\S+\s+([^aeiou]*[aeiou]){2}[^aeiou]*\b' file

perl (honestly, I did this independent of steeldriver's comment)

perl -ane 'print if (lc($F[1]) =~ tr/aeiou/aeiou/) == 2' file


awk '{col2 = tolower($2); gsub(/[aeiou]/,"",col2)} length($2) - length(col2) == 2' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.