用grep / egrep查找重复的单词

时间:2016-12-14 20:33:55

标签: grep words repeat

我需要用grep / egrep找到重复的单词。该计划如下:

空格word1< 1个或更多个空格> word1< 2个或更多个空格> word2< 1个或更多个空格> word2空间

示例

hello world world yaaay yaaay abc

grep之后:"世界世界yaaay yaaay"

我尝试使用:egrep " (\w{1,}) {1,}\1 "

1 个答案:

答案 0 :(得分:0)

我可以使用awk

来实现这个目标
echo "hello world world yaaay yaaay abc" | awk 'BEGIN{tmp=""; result=""} {for(x=1;x<=NF;x++){if($x==tmp){result = result " "  $x " " $x};tmp=$x}} END{print result}'

输出:

 world world yaaay yaaay

用字符串“hello world world yaaay yaaay wow aaa aaa abc”再测试一次:

echo "hello world world yaaay yaaay wow aaa aaa abc" | awk 'BEGIN{tmp=""; result=""} {for(x=1;x<=NF;x++){if($x==tmp){result = result " "  $x " " $x};tmp=$x}} END{print result}'

输出:

世界世界yaaay yaaay aaa aaa