Parsing XML using unix terminal

By : Mattias

Sometimes I need to quickly extract some arbitrary data from XML files to put into a CSV format. What's your best practices for doing this in the Unix terminal? I would love some code examples, so for instance how can I get the following problem solved?

Example XML input:

<myel name="Foo" />
<myel name="Bar" />

My desired CSV output:

By : Mattias


Answering the original question, assuming xml file is "test.xml" that contains:

<root> <myel name="Foo" /> <myel name="Bar" /> </root>

cat text.xml | tr -s "\"" " " | awk '{printf "%s,\n", $3}'

XMLStarlet is a command line toolkit to query/edit/check/transform XML documents (for more information see

No files to write, just pipe your file to xmlstarlet and apply an xpath filter.

cat file.xml | xml sel -t -m 'xpathExpression' -v 'elemName' 'literal' -v 'elname' -n -m expression -v value '' included literal -n newline

So for your xpath the xpath expression would be //myel/@name which would provide the two attribute values.

Very handy tool.


By : DaveP

This video can help you solving your question :)
By: admin