# The test file
$ cat test
abcabc
abc
# Match exactly two occurrences of 'abc'
$ grep -E '(abc){2}' test
abcabc
# Match one ore more occurrences of 'abc'
$ grep -E '(abc)+' test
abcabc
abc
You can use POSIX-compatible regular expressions with egrep or grep -E:
# The test file
$ cat test
abcabc
abc
# Match exactly two occurrences of 'abc'
$ grep -E '(abc){2}' test
abcabc
# Match one ore more occurrences of 'abc'
$ grep -E '(abc)+' test
abcabc
abc
我对 joschi 的回答有补充: 如果您不知道mybigsentence,但您想搜索最小长度的任意重复字符串(我在示例中假设长度为 10 个字符),您可以使用 GNU egrep< /code> 像这样:
egrep -on '([a-z]{10,})\1' myfile
这将返回匹配的行号(-n)和匹配本身(-o),但不是整行(您会无需 -o 即可获取)。
但这仅适用于 GNU 版本的 grep。
I have an addition to joschi's answer: If you don't know mybigsentence but you want to search for arbitrary repeated strings of a minimum length (I assume a length of 10 characters in my example) you could do it with GNU egrep like this:
egrep -on '([a-z]{10,})\1' myfile
This will return the line number (-n) of the match and the match itself (-o) but not the whole line (which you would get without -o).
But that will only work with the GNU version of grep.
发布评论
评论(2)
您可以将 POSIX 兼容的正则表达式与
egrep
或grep -E
结合使用:You can use POSIX-compatible regular expressions with
egrep
orgrep -E
:我对 joschi 的回答有补充:
如果您不知道mybigsentence,但您想搜索最小长度的任意重复字符串(我在示例中假设长度为 10 个字符),您可以使用 GNU
egrep< /code> 像这样:
这将返回匹配的行号(
-n
)和匹配本身(-o
),但不是整行(您会无需-o
即可获取)。但这仅适用于 GNU 版本的
grep
。I have an addition to joschi's answer:
If you don't know mybigsentence but you want to search for arbitrary repeated strings of a minimum length (I assume a length of 10 characters in my example) you could do it with GNU
egrep
like this:This will return the line number (
-n
) of the match and the match itself (-o
) but not the whole line (which you would get without-o
).But that will only work with the GNU version of
grep
.