Skip to main content
Post Reopened by 200_success, AdminBee, Chris Davies, αғsнιη, G-Man Says 'Reinstate Monica'
Formatting
Source Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

If you want to replace a keyword with a string using sedsed, sedsed tries hard to interpret your replacement string. If the replacement string happens to have characters that sedsed considers special, like a '/'/ character, it will fail, unless of course you meant your replacement string to have characters that tell sedsed how to act.

Ex:

VAR="hi/"

sed "s/KEYWORD/$VAR/g" somefile

Is there any way to tell sedsed not to try to interpret the replacement string for special characters? All I want is to be able to replace a keyword in a file with the contents of a variable, no matter what that content is.

If you want to replace a keyword with a string using sed, sed tries hard to interpret your replacement string. If the replacement string happens to have characters that sed considers special, like a '/' character, it will fail, unless of course you meant your replacement string to have characters that tell sed how to act.

Ex:

VAR="hi/"

sed "s/KEYWORD/$VAR/g" somefile

Is there any way to tell sed not to try to interpret the replacement string for special characters? All I want is to be able to replace a keyword in a file with the contents of a variable, no matter what that content is.

If you want to replace a keyword with a string using sed, sed tries hard to interpret your replacement string. If the replacement string happens to have characters that sed considers special, like a / character, it will fail, unless of course you meant your replacement string to have characters that tell sed how to act.

Ex:

VAR="hi/"

sed "s/KEYWORD/$VAR/g" somefile

Is there any way to tell sed not to try to interpret the replacement string for special characters? All I want is to be able to replace a keyword in a file with the contents of a variable, no matter what that content is.

Post Closed as "Needs more focus" by Wildcard, cuonglm, Jakuje, Anthon, jimmij
Source Link
Tal
  • 2.3k
  • 5
  • 26
  • 39

Is there a way to prevent sed from interpreting the replacement string?

If you want to replace a keyword with a string using sed, sed tries hard to interpret your replacement string. If the replacement string happens to have characters that sed considers special, like a '/' character, it will fail, unless of course you meant your replacement string to have characters that tell sed how to act.

Ex:

VAR="hi/"

sed "s/KEYWORD/$VAR/g" somefile

Is there any way to tell sed not to try to interpret the replacement string for special characters? All I want is to be able to replace a keyword in a file with the contents of a variable, no matter what that content is.