-2

I have an use case, where i need to replace a placeholder using a string having [] and a $ and - inside that.

example [- $the_real_ip]/@@log_pattern@@ . i need to replace my log pattern using [- $the_real_ip] using sed. let me know if there is any option to do that.

akhinair
  • 73
  • 1
  • 7
  • 3
    Does this answer your question? [Escape a string for a sed replace pattern](https://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern) – l3l_aze Jul 14 '20 at 22:27
  • Also [Replace one substring for another string in shell script](https://stackoverflow.com/q/13210880/7665043). Top answer is a "Bashism" that doesn't use `sed`, if your script is meant to be Bash-only. – l3l_aze Jul 14 '20 at 22:34
  • my intention is use sed for the replacement. – akhinair Jul 14 '20 at 22:40
  • The top answer of the first link above is related to escaping special characters so they're not interpreted by the shell before sed is ran, and links to a much longer answer which is more like an article. – l3l_aze Jul 14 '20 at 22:59

1 Answers1

0

I can't see why just escaping them wouldn't work.

sed 's@\[\- \$the_real_ip\]@log_pattern@' file
petrus4
  • 616
  • 4
  • 7