i want to know how to count the number of words in a .txt file that has more than 5 characters, using egrep and wc. Please, be clear as i'm new to linux.

  • Have you tried some code? Where do you face difficulties?
    – thanasisp
    May 14 at 20:29
  • Does it have to be with just those two?
    – ilkkachu
    May 14 at 20:35
  • @thanasisp only know how to count the words cat file.txt | wc -w, i'm having a problem filtering the length of the word May 14 at 20:48
  • @ilkkachu according to my professor it has to contain egrep, but the wc is opcional May 14 at 20:50

1 Answer 1


After you mentioned your professor, I'm a bit worried we're doing your homework for you, but you can try:

egrep -wo '[[:alnum:]]{5,}' filename.txt | wc -w 

This looks for spans of 5 or more alphanumeric characters in a row forming a word (-w), prints only the matches, each on its own line (-o) and then counts them with wc -w. (wc -l would also work in this case).

Change [[:alnum:]] to [[:alpha:]] if only letters should count.

If for some reason you want only to use egrep, you can call it twice, and have the second one count any line with anything in it.

egrep -wo '[[:alnum:]]{5,}' filename.txt | egrep -c '.'

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.