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
add comment

1 Answer

up vote 2 down vote accepted

Two tips:

  1. You can't escape a single quote within a single quote. So you have to close the quote, add an escaped quote, then open the quotes again. That is: 'fooo'\''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 these files:

$ 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

If you ignore the funky features I used, this translates to: s/['"]//g. It means remove all characters matching either ' or ". 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
add comment

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.