I have a text file and I am using the grep command with a regular expression to get only the lines which contain three same successive letters, e.g.: aaa bbb ccc ddd
What regular expression do I need to use in : grep "regex" filename
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityprintf 'aabbbccddd\nabcdef' | grep '\([a-z]\)\1\1'
Output: aabbbccddd
The bracket pair \(\)
makes a backreference, which is referenced by \1
[a-z]
. \1
means we are back-referencing to the group index 1 (ie, we are referring the characters stored inside the group index 1)
Feb 15, 2015 at 13:06
using grep
echo -e "aaa bbb ccc ddd\n hello world"|egrep '([a-z])\1{2}'
([a-z])
remembers the first letter found.
\1{2}
check to see if the first letter found is repeated two more times.
grep '\([a-z]\)\1\{2\}'
(BRE) would be POSIX/portable.
Feb 24, 2015 at 22:41
echo " heeeloo"|egrep -o --color '([a-z])\1{2}'
..this will output the pattern eee
,that is, the same output of grep '\([a-z]\)\1\{2\}'
grep aaa
not get you? Or do you want:grep '\([[:alpha:]]\)\1\1'
?grep '\([[:alpha:]]\)\1\{2\}'
.aaaa
line also? Because this contains three consecutive a's.