How to remove XML tags from Unix command line?

Tarski picture Tarski · Mar 21, 2011 · Viewed 34.4k times · Source

I am grepping an XML File, which gives me output like this:

<tag>data</tag>
<tag>more data</tag>
...

Note, this is a flat file, not an XML tree. I want to remove the XML tags and just display the data in between. I'm doing all this from the command line and was wondering if there is a better way than piping it into awk twice...

cat file.xml | awk -F'>' '{print $2}' | awk -F'<' '{print $1}'

Ideally, I would like to do this in one command

Answer

Johnsyweb picture Johnsyweb · Mar 21, 2011

If your file looks just like that, then sed can help you:

sed -e 's/<[^>]*>//g' file.xml

Of course you should not use regular expressions for parsing XML because it's hard.