'Grep command to find same number digits

What i am looking for is to find a grep command to find same number digits in a 4 digit sequence.

For example

1111  
1234  
5678  
1231  
2233  
4546  

output should be

1111  
1231  
2233  
4546  

Ive tried grep "\([0-9]\)\\1" but that doesnt catch numbers like 1231 or 4546 thanks



Solution 1:[1]

Using grep

$ grep '\([0-9]\).*\1' file
1111
1231
2233
4546

Match interger character in any position in the first cature group within the parenthesis. If the number captured is found again as a last occurance with the back reference \1, then a match is made.

Back-References And Subexpressions

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1