How to write complex sed query which got special char

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: ../../../profileoneString 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.

2

2 Answers

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

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'
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like