1

I can do this for example

sed 's/\>\</\>\
</g'

To replace >< with

>
<

However, say I want a variable, like this:

 sed 's/\>\</\>\
<$1/g'

It will interpret it as literally $1. To fix this, I use " instead of '.

Like this:

sed "s/\>\</\>\
<$1/g"

But it doesn't understand the newline when I use ". \n does not work either.

8
  • "it doesn't understand the newline".. does it understand \n?
    – muru
    Dec 12, 2015 at 0:32
  • @muru No, I already said that. It interprets it as a literal \n. Same thing with -e if that would change anything. Dec 12, 2015 at 0:33
  • Yep, didn't notice that. Which shell are you using? sed will only get the pattern after the shell does quote removal, etc., so if quoting causes a problem, the shell is probably the culprit.
    – muru
    Dec 12, 2015 at 0:36
  • @muru I am using bash. Dec 12, 2015 at 0:39
  • What if you do 's/></>\[enter]<'"$SHELL/g"?
    – muru
    Dec 12, 2015 at 0:44

1 Answer 1

2

I'm not sure what quoting mystery is behind this, but if single quotes help with the newline, you can mix and match with double quotes for the variable:

sed 's/></>\
<'"$1/g"
1
  • 1
    double-quotes eat backslash escaped newlines. "This" == "T\\nhis"
    – mikeserv
    Dec 12, 2015 at 17:19

Your Answer

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

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