Search code examples
bashshellgrep

Find words containing at least three instances of a letter


Ex: Find words in "words.txt" with at least three a or A (both capital and small) letters. (Hint: You should probably use cat command, then, feed its output into grep command.)

I have tried some ways but I found like ___sss__, eee___.

My list:

Ababdeh
Ababua
abac
abaca
abacate
abacay

I have to find:

Ababua
abaca
abacate
abacay

Could you help me please?


Solution

  • Use a capturing group and backreferences.

    grep -Ei '(.).*\1.*\1' words.txt
    

    If there is a certain letter to look for, let's say a, that's easier.

    grep -i 'a.*a.*a' words.txt