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.
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 theg
flag to replace multiple repeating word patterns.