I have a file that contains multiple lines of xml. I would like to replace certain parts of the file. Some parts of the file contains quotation marks (") which I would like to replace. I have been trying to escape the quotation mark with \, but I don't think this is working based on the result of my file.

Here is an example of one of my sed commands:

sed -e "s/\"text\"/'text'/ig" file.xml > temp.tmp

Is this how you escape quotation marks in a sed command or am I doing something wrong?

  • 2
    Your command looks right for replacing "text" with 'text'. Of course it won't do anything to "othertext". Show a few input lines, the corresponding undesired output, and explain what output you want instead. Mar 20, 2011 at 19:11
  • So \" is the correct way of escaping quotation marks in sed command?
    – jbranchaud
    Mar 20, 2011 at 19:15
  • 4
    Not for sed: sed doesn't need, or support, escaping ". But your shell command uses a double-quoted string, and \" is correct there. The sed program sees s/"text"/'text'/ig as the argument to -e. Mar 20, 2011 at 19:20
  • @Gilles What about spaces? Does sed understand and respect white spaces? For instance, if my command contained s/\"text\" /'text'/ig Would it find only "text" with the space after it?
    – jbranchaud
    Mar 20, 2011 at 19:24
  • 3
    Spaces must match exactly. Rather than continue this dialogue, I recommend that you post some sample input and the corresponding desired output (and perhaps explain why you need to change the quoting). It's not even clear that sed is the right tool for the job, maybe you want an XML parser. Mar 20, 2011 at 19:28

2 Answers 2


Two tips:

  1. You can't escape a single quote within a string quoted with single quotes. So you have to close the quote, add an escaped quote, then open the quotes again. That is: 'foo'\''bar', which breaks down as:

    • 'foo'        quoted foo
    • \'             escaped '
    • 'bar'        quoted bar

    yielding foo'bar.

  2. (optional) You don't necessarily have to use / in sed. I find that using / and \ in the same sed expression makes it difficult to read.

For example, to remove the quotes from this file:

$ cat /tmp/f

Given my two tips above, the command you can use to remove both double and single quotes is:

$ sed -e 's|["'\'']||g'  /tmp/f

Based on my first tip, the shell reduces sed's second argument (i.e., the string after the -e) to s|["']||g and passes that string to sed. Based on my second tip, sed treats this the same as s/['"]//g. It means

remove all characters matching either ' or "   (i.e., replace them with nothing)

You probably need something more complex than this to do what you want, but it's a start.

  • 1
    To put a finer point on your second tip: you can use any character in place of / when using the s and y commands, possibly among others. When using regexps with other sed commands, the first delimiter (if using an alternative to /) must be escaped. Your preferred delimiter must also be escaped if you are trying to match it within the regexp.
    – Eli Heady
    Mar 20, 2011 at 22:41
  • It’s hard to mix single quotes and double quotes without getting messy. Some people find it easier to read if you quote single quotes, by putting them in double quotes, rather than escaping them. So, rather than 'foo'\''bar', we might use 'foo'"'"'bar'. Jul 28, 2014 at 21:30

I have a Windows port of unix utilities so commands look a bit different but I had a csv file with commas and quote marks. Using this thread as a guide, I was able to remove the quotes via this command:

c:\Temp> cat report.csv | sed "s/\,/\ /g" | sed "s/[""]//g"
  • thanks! was stuck on this! Apr 18, 2020 at 18:22
  • Try this -- less typing: c:\Temp> cat report.csv | sed "s/["",]//g"
    – Bob
    Aug 11, 2020 at 18:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .