sed - search and replace whole string which contains dot

Hello.

I would like to search exactly "string1.string2.string3" and replace it by "new_string1.new_string2.new_string3"
And
I would like to search exactly "string2.string3" and replace it by "new_string2.new_string3"
And
I would not found in the result : "string1.new_string2.new_string3" because "string2.string3" is different from "string1.string2.string3"

"string1.string2.string3" are email address like firstname\_1.firstname_2.name@subdomain.domain.ext

"string2.string3" are email address like firstname.name@subdomain.domain.ext

So if size is different, a match should not apply in "string1.string2.string3" when search is done with "string2.string3"

How to do that ?

sed '/\<tring1.string2.string3\>/something_else/g'    # dose not work.
sed '/\btring1.string2.string3\b/something_else/g'     # dose not work.
sed '/\<tring1\.string2\.string3\>/something_else/g'  # dose not work.

Any help is welcome

Hi,
A possible syntax, but line mustn't begin with the string to search (not tested):

sed 's/\([[:space:]]\)string1\.string2\.string3@/\1new_string1.new_string2.new_string3@/g'

Regards.

1 Like

You need to specify the character(-set) that is before the beginning of the string.
If there is nothing before - the string is at the beginning of the line - then give ^.

sed '/^string1\.string2\.string3@/something_else@/'

The same with the character after the end that is @ (and because it is cut it needs to be added again).

1 Like

I guess the longer search string is not the problem as it would not modify the shorter one; it is vice versa. The real discriminator is the dot in front of "string2". Try

sed 's/[^.]string2\.string3/new_string2.new_string3/g' file

It would leave three element addresses alone. You may want to add the "@" to the end to make sure it only acts on e-mail addresses.