Skip to main content
2 of 3
added sed code template
Joe
  • 1.4k
  • 1
  • 17
  • 20

One way to get around this would be to use multiple sed steps to get all the double quotes out of the way.

In the first one you'd replace all double quotes in the target file with a tag character such as @. It has to be a character or string guaranteed not to be present in the original.

Then, you'd do your substitution using source and target strings like your originals, but with all the quotes replaced with the tag character in both strings.

Finally, you'd replace all tag characters with double quotes.

As for the .*, it's sort of a cake and eat it situation. If it's not quoted, bash will eat it and if it is quoted, sed will see it as a literal. You may have to rewrite this part using a regex that will survive being quoted in bash but take effect in sed.

That's why I said it would probably be easier in awk. ;) Especially the last part!

If you get the sed steps to work separately - so you can make sure each one works, you can recombine them to do all the edits in one sed invocation. A clean way to do that is:

sed -i '
    s/first target/first replacement/
    s/second target/second replacement/
    s/third target/third replacement/
' input-file
Joe
  • 1.4k
  • 1
  • 17
  • 20