Home > Enterprise >  Trying to remove duplicates words in a String using sed of MacOS
Trying to remove duplicates words in a String using sed of MacOS

Time:01-02

I'm trying to remove duplicates words in a string using sed from MacOS using the following command:

sed -r 's/^([A-Za-z0-9_] ) \1$/\1/' <<< 'The best of of The United Kingdom'

But it only returns

The best of of United Kingdom

What I'm missing? Could you guys give me hand? Please.

CodePudding user response:

You can try this sed

$ sed 's/\([^ ]* \)\1\ /\1/' input_file
The best of The United Kingdom

Your original code had unneeded anchors ^|$. Here is a fixed version

$ sed -r 's/([A-Za-z0-9_]* )\1 /\1/' <<< 'The best of of The United Kingdom'
The best of The United Kingdom

CodePudding user response:

I installed gnu-sed. Problem solved.

CodePudding user response:

You are unnecesarily anchoring the regex at the start and end of line. Remove ^ and $. Change -r to the POSIX -E and it will work on BSD/Mac sed. You also need the g flag to replace multiple repeating word patterns.

sed -E 's/([A-Za-z0-9_] ) \1/\1/g'
  • Related