user1701840
user1701840

Reputation: 1112

how to find words that has 2 consecutive same characters using egrep?

I tried to do it the brute force way:

egrep '[a][a] | [b][b] | [c][c]....' file

is there any better way to do it?

Upvotes: 2

Views: 3578

Answers (2)

Aster
Aster

Reputation: 233

You can also do this:

$ egrep '([a-z])\1' file

([a-z]) - for any character assigns it a number with in the pattern, I believe the numbering starts from 0, and then you are checking if the second letter is same as 1.

Upvotes: 2

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137418

Use a back-reference:

egrep '(\w)\1' file will match any two consecutive "word characters".

The (\w) creates a "group" that matches any word character. The \1 refers to whatever was matched in the first group (which here means the previous character.

$ cat test
abcdefg
1234567
abcddefgj
88427

$ egrep '(\w)\1' test
abcddefgj
88427

Upvotes: 8

Related Questions