Allen Hu
Allen Hu

Reputation: 113

how to write regular expression to match the repeated strings and designate the repeated number?

for example, i want to match the string "abcabc" in a text file, where the two (and only two) "abc" are attached together, and no characters are in front of and at the end of "abcabc"?

if i use grep -n '(abc){2}' TEST, it does not work

Upvotes: 0

Views: 109

Answers (5)

Allen Hu
Allen Hu

Reputation: 113

Thanks guys, from your answers, i think i have got what i need.

As i said in my questions, i want to match "abcabc", where 2 and only repeated "abc", no other characters in front of and at the end of, but "abcabc" is not the only string in that line.

so the answers is:

grep -n "\b\\(abc\\)\\{2\\}\b" filename

Upvotes: 0

tuxuday
tuxuday

Reputation: 3037

Check this fits.

$ grep '\<abcabc\>'
test
abcabc
abcabc
test abcabc
test abcabc
testabcabc

Upvotes: 0

codaddict
codaddict

Reputation: 455192

Try:

egrep '\b(abc){2}\b' input

Upvotes: 1

Emil Vikstr&#246;m
Emil Vikstr&#246;m

Reputation: 91963

This is the shortest alternative:

^abcabc$

Upvotes: 1

guido
guido

Reputation: 19204

Escape the parentesis:

grep -n '\(abc\)\{2\}' TEST

if you want to match the string abcabc alone on a line, as your description seems to suggest, use:

grep -n '^\(abc\)\{2\}$' TEST

Upvotes: 1

Related Questions