Issue
I am trying to replace a word in a string which contains same word with special character in it.
Example:
string="this is a joke. this is a poor-joke. this is a joke-club"
I just want to replace the word joke with coke, not with the special character.
below command replaces all the word joke.
[chandu@mynode ~]$ echo $string | sed "s/joke/coke/g;"
this is a coke. this is a poor-coke. this is a coke-club
I tried using sed "s/\<joke\>/coke/g;"
but even this replaces all the words
Expected output:
this is a coke. this is a poor-joke. this is a joke-club
Solution
You can match beginning and ending of the word yourself if you want to include -
as word character.
$ sed 's/\(^\|[^a-zA-Z-]\)joke\([^a-zA-Z-]\|$\)/\1coke\2/g' <<<"$string"
this is a coke. this is a poor-joke. this is a joke-club
Answered By - KamilCuk