Skip to content
Advertisement

How can I find the number of 8 letter words that do not contain the letter “e”, using the grep command?

I want to find the number of 8 letter words that do not contain the letter “e” in a number of text files (*.txt). In the process I ran into two issues: my lack of understanding in quantifiers and how to exclude characters.

I’m quite new to the Unix terminal, but this is what I have tried:

cat *.txt | grep -Eo "w+" | grep -i ".*[^e].*"

I need to include the cat command because it otherwise includes the names of the text files in the pipe. The second pipe is to have all the words in a list, and it works, but the last pipe was meant to find all the words that do not have the letter “e” in them, but doesn’t seem to work. (I thought “.” for no or any number of any character, followed by a character that is not an “e”, and followed by another “.” for no or any number of any character.)

cat *.txt | grep -Eo "w+" | grep -wi "[a-z][a-z][a-z][a-z][a-z][a-z][a-z][a-z]"

This command works to find the words that contain 8 characters, but it is quite ineffective, because I have to repeat “[a-z]” 8 times. I thought it could also be “[a-z]{8}”, but that doesn’t seem to work.

cat *.txt | grep -Eo "w+" | grep -wi "[a-z][a-z][a-z][a-z][a-z][a-z][a-z][a-z]" | grep -i ".*[^e].*"

So finally, this would be my best guess, however, the third pipe is ineffective and the last pipe doesn’t work.

Advertisement

Answer

You may use this grep:

grep -hEiwo '[a-df-z]{8}' *.txt

Here:

  • [a-df-z]{8}: Matches all letters except e
  • -h: Don’t print filename in output
  • -i: Ignore case search
  • -o: Print matches only
  • -w: Match complete words
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement