6

I am trying to replace a string inside a text file with a variable.

I am using this code:

sed -i "s/search text \/TAG\//replace text ${variable}/" ${textfile}

The content of the variable is /newTAG/. And the content of the variable is the problem, more accuratelyte the / at the beginning and the end.

When both / are removed so the content is only newTAG everything works fine.

I already tried the following

sed -i "s/search text \/TAG\//replace text "${variable}"/" ${textfile}
sed -i 's/search text \/TAG\//replace text "${variable}"/' ${textfile}

Can anybody help me? Thanks

9

Use something other than / as the delimiter for the regex (for example, the colon :):

sed -i "s:search text /TAG/:replace text ${variable}:" "${textfile}"

Or escape the / in the variable.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.