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?
Best Answer
Two tips:
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'
quotedfoo
\'
escaped'
'bar'
quotedbar
yielding
foo'bar
./
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:
Given my two tips above, the command you can use to remove both double and single quotes is:
Based on my first tip, the shell reduces sed's second argument (i.e., the string after the
-e
) tos|["']||g
and passes that string to sed. Based on my second tip, sed treats this the same ass/['"]//g
. It meansYou probably need something more complex than this to do what you want, but it's a start.