2

I am struggling to replace a string with a special character. I am using the command below and I tried to escape each special character, but I am still getting an error.

If I don't use special characters, the query is working fine, but I have to use special characters.

String to find: ../../../profileone String to Replace: @mysettings/future-system-one

Query:

sed -i s/"../../../profileone"/"@mysettings/future-system-one"/g *.fileExtension'

I wanted to try this command from jenkins pipeline.

terdon
  • 104,119

2 Answers2

2

Following the comment by @steeldriver, you can change the pattern delimiters from / to _:

sed -i s_"../../../profileone"_"@mysettings/future-system-one"_g *.fileExtension'

so that you don't need to escape all the / in this way:

sed -i s/"..\/..\/..\/profileone"/"@mysettings\/future-system-one"/g *.fileExtension'
mattb
  • 394
2

You don't actually have any special characters there, so you don't need to escape anything. The only issue is that you are using / as the pattern delimiter, so just use another character and it should work fine:

sed 's|../../../profileone|@mysettings/future-system-one|g' *.fileExtension

Note how the sed command is quoted, that is important.

Now, your question shows the target strings as ../../../profileone and @mysettings/future-system-one, but your command also includes double quotes. If those are supposed to be part of the strings, use this instead:

sed 's|"../../../profileone"|"@mysettings/future-system-one"|g' *.fileExtension
terdon
  • 104,119