XML Parse with Shell Script

2

I have the following structure inside an XML file:

<TestCase name="A" priority="">
  <Test name="A1" result="pass" />
  <Test name="A2" result="pass"/>
</TestCase>
<TestCase name="B" priority="">
  <Test name="B1" result="pass" />
  <Test name="B2" result="pass"/>
</TestCase>

Using shell script, how do I read and navigate the tags and their subtags, in order to print the result:

A
 A1 - pass
 A2 - pass
B
 B1 - pass
 B2 - pass
    
asked by anonymous 19.02.2015 / 18:14

1 answer

1

Your example is not well-formed XML ...

To process XML you may want to use a tool that includes an XML Parser. For line command I would suggest something with xmllint or xmlstarlet (there are others excellent)

Using regular expressions you can also do anything if the file is very simple and regular. Then there is a Perl example (one can do something similar in sed, awk, python) but that is clinging to the example presented ...

perl -nE '/name="(.*?)".*result="(.*?)"/ and say " $1 - $2"  or
          /name="(.*?)"/                 and say $1'    file.xml
    
25.02.2015 / 09:49