Escape < and > in sed/shell
Question
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.
Solution
Ok. Found out by myself. Use quotes.
$ sed -i "s/>foo</>bar</g" file
OTHER TIPS
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
Escape them with backslash
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow