Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
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. –  Gilles Mar 20 '11 at 19:11
    
So \" is the correct way of escaping quotation marks in sed command? –  jbranchaud Mar 20 '11 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. –  Gilles Mar 20 '11 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 '11 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. –  Gilles Mar 20 '11 at 19:28

1 Answer 1

up vote 4 down vote accepted

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
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. I hope it helps.

share|improve this answer
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 '11 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'. –  Scott 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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