I want to find 3 consecutive characters and then another character or the end of the line. I tried this:

egrep '(.)\1{2}' file

but I need less results.

  • 2
    It works fine for me. If it is still not working for you, please show (a) some sample input, (b) the output that you see, and (c) your desired output. – John1024 May 20 at 19:49
  • What is the another char or end of line supposed to restrict? The only case I can see is a file which ends exactly with the 3 identical characters, and that is not a text file - a text file is either empty or it ends with a newline on unix. Are you trying to restrict to exactly 3 consecutive characters, so this is more aaaa letter a would not match? Note in that example the last three characters in aaaa and the following space match your requirement of 3 consecutive characters and then another char. – icarus May 20 at 20:02

Can't make this work with grep, but I can make it work with egrep:
egrep -o '([[:print:]\d])\1\1([[:print:]]|$)'
That will match 3 consecutive characters, followed by either white space, a single printable character, or an end of line character.

Note: The -o prevents the issue of aBBBf matching and will show only BBBf.

  • 3
    egrep is simply grep -E – Panki May 21 at 7:14
  • Yes. egrep is grep -E on "new" Unix. In my "old school days," I don't remember egrep being an alias to grep -E. Since the OP didn't say when flavor of Unix he was using, and appears to be a non-native English speaker. Wouldn't you agree it's best to stick to old-school POSIX because we know it will work? – Scottie H May 22 at 6:58

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.