Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect repeated characters using grep

Tags:

linux

grep

bash

I'm trying to write a grep (or egrep) command that will find and print any lines in "words.txt" which contain the same lower-case letter three times in a row. The three occurrences of the letter may appear consecutively (as in "mooo") or separated by one or more spaces (as in "x x x") but not separated by any other characters.

words.txt contains:

The monster said "grrr"!
He lived in an igloo only in the winter.
He looked like an aardvark.

Here's what I think the command should look like:

grep -E '\b[^ ]*[[:alpha:]]{3}[^ ]*\b' 'words.txt'

Although I know this is wrong, but I don't know enough of the syntax to figure it out. Using grep, could someone please help me?

like image 981
Unknown Avatar asked Oct 28 '25 01:10

Unknown


1 Answers

Does this work for you?

grep '\([[:lower:]]\) *\1 *\1'

It takes a lowercase character [[:lower:]] and remembers it \( ... \). It than tries to match any number of spaces _* (0 included), the rememberd character \1, any number of spaces, the remembered character. And that's it.

You can try running it with --color=auto to see what parts of the input it matched.

like image 91
choroba Avatar answered Oct 30 '25 17:10

choroba