20

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?

5
  • 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. Commented Mar 20, 2011 at 19:11
  • So \" is the correct way of escaping quotation marks in sed command?
    – jbranchaud
    Commented 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. Commented 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
    Commented 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. Commented Mar 20, 2011 at 19:28

3 Answers 3

21

Two tips in Bourne-like shells:

  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
aaa"bbb"'ccc'aaa

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.


¹ Except in zsh (primarily a Bourne/Korn-like shell even though it does also have a few features from csh/tcsh (like bash) and rc) when the rcquotes option is enabled after which you can escape single quotes as '' like in the rc shell or derivatives, as in echo 'There''s a single quote in that single-quoted string'.

2
  • 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
    Commented 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'. Commented Jul 28, 2014 at 21:30
0

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

Use a quantifier (+) ater the double quote in the search side only.

The question mark (?) is also a quantifier but makes optional, it could be used if you want the quote in the result even if the search has or not a quote at that position (?).

The general answer is using ("+) that means "at least one".

 sed -i -E 's/text"+hi/text"no/g' onefile

 # will change text"hi by text"no

You must log in to answer this question.

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