tags:

views:

1106

answers:

4

How do I escape '<' and '>' character in sed.

I have some xml files which needs some text between the tags to be replaced. How do I escape the '>' and '<' characters.

The problem with > and < is it has special meaning in the shell to redirect the output to a file. So backslash doesn't work.

A: 

Escape them with backslash

Milan Babuškov
A: 

Just put a backslash before them or enclose them in single or double quotes. On second thought, your question I think needs to be more clear. Are you trying to use sed to process an XML file and you want to get what's between a tag? Then you want:

sed -re 's@(<TAG.*?>).*?(</TAG>)@\1hi\2@' test.xml
Douglas Mayle
I dont want to process the xml file. Just replace some text between the <tag>foobar</tag>. Anyway thanks.
cnu
+3  A: 

Ok. Found out by myself. Use quotes.

$ sed -i "s/>foo</>bar</g" file
cnu
A: 

I want to substitute 'foo' with 'bar' in line containing 'pippo' and 'minnie'. How can I do it with sed?

giulia
This isn't an answer. Use the **[Ask Question]** button at the top right of the screen if you want to ask a question.
Bill the Lizard