Search code examples
grepwordsrepeat

Find repeated words with grep/egrep


I need to find repeated words with grep/egrep. The scheme is the following:

space word1<1 or more spaces>word1<2 or more spaces>word2<1 or more spaces>word2 space

Example

hello world world yaaay yaaay abc

After grep: " world world yaaay yaaay "

I tried to use: egrep " (\w{1,}) {1,}\1 "


Solution

  • I can achive this with 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}'
    

    Output:

     world world yaaay yaaay
    

    One more test with string "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}'
    

    Output:

    world world yaaay yaaay aaa aaa