這應該爲你工作:
grep -E "[[:<:]](\w+)[[:>:]].*[[:<:]]\1[[:>:]].*[[:<:]]\1[[:>:]]" testfile
例如:
[email protected]:~/src/sandbox$ cat testfile
how is summer summer summer ha ha
this summer is a hot summer of summers yes it is
summer summer summer
there is only one summer in this sentence
summer appears as the first and last summer words in this summer
the summertime is always in summer, one of several summers
the summer of which we speak is summery but is a real summer summer, yes
this also works with cats, since there are three cats in these cats, ha!
[email protected]:~/src/sandbox$ grep -E "[[:<:]](\w+)[[:>:]].*[[:<:]]\1[[:>:]].*[[:<:]]\1[[:>:]]" testfile
how is summer summer summer ha ha
summer summer summer
summer appears as the first and last summer words in this summer
the summer of which we speak is summery but is a real summer summer, yes
this also works with cats, since there are three cats in these cats, ha!
[email protected]:~/src/sandbox$
[[:<:]]
和[[:>:]]
比賽在一個字,分別的開始和結束的空字符串,這樣你就可以使用它們確定單詞的邊界,而不必假設單詞之間用空格分隔,而不是用標點符號等。
但它*會捕獲晚餐的例子。它是否應匹配由其他詞分隔的三個詞? –
是的,你是對的。將它改爲'午餐午餐晚餐晚餐午餐',不會被抓到。 – user313967