0

Suppose an XML file spam.xml is like

<spam>
<victims>
  <victim>
    <name>The Pope</name>
    <email>pope@vatican.gob.va</email>
    <is_satan>0</is_satan>
  </victim>
  <victim>
    <name>George Bush</name>
    <email>father@nwo.com</email>
    <is_satan>1</is_satan>
  </victim>
  <victim>
    <name>George Bush Jr</name>
    <email>son@nwo.com</email>
    <is_satan>0</is_satan>
  </victim>
</victims>
</spam>

How can I find and display the email address of George Bush Jr, from a shell script.

The use of a tool like xmllint or xml2 would be appreciated but not essential.

Edit: I'm sorry, I guess somehow I didn't make the question clear enough. I know I can run something like

$ xmllint --xpath '//spam/victims/victim/name' spam.xml
<name>ThePope</name><name>George Bush</name><name>George Bush Jr</name>

but that's not what I am after. I want to search for the name and have returned the email. Maybe it is not possible, maybe I have to write an extended sed/awk/bash script to do this. I was hoping with all the hype around XML there was a clever and simple way to do it, but maybe not. I am not familiar with XML, that's why I ask :)

Dokbua
  • 213
  • 3
  • 12
  • 1
    I'd suggest looking here: https://stackoverflow.com/questions/15461737/how-to-execute-xpath-one-liners-from-shell – bsivo Aug 09 '17 at 15:09
  • 1
    You just need a slightly more complex xpath query. Like `//spam/victims/victim[name='George Bush']/email` – o11c Aug 09 '17 at 21:58
  • Awesome! Exactly what I was looking for, thanks! – Dokbua Aug 09 '17 at 22:08

0 Answers0