0
<xml>
    <email>asdadw@cutshort.io</email>
    <email>sadfasd@cutshort.io</email>
    <email>asdas@cutshort.io</email>
    <host>
        <ip>56.456.155.423, 54.455.155.654, 544.47.745.80, 54.192.155.842</ip>
        <hostname>abc.cutshort.io</hostname>
    </host>
    <host>cdn.cutshort.io</host>
    <host>
        <ip>56.456.155.423, 54.455.155.654, 544.47.745.80, 54.192.155.842</ip>
        <hostname>abc.cutshort.io</hostname>
    </host>
    <host>www.cutshort.io</host>
    <host>blogdata.cutshort.io</host>
    <host>cdn.cutshort.io</host>
</xml>

I want to extract the value of "host" without extracting the value of "hostname" or "ip". The output is supposed to look like this:

cdn.cutshort.io
www.cutshort.io
blogdata.cutshort.io
cdn.cutshort.io

I have only recently started xml parsing so please be patient. Any help will be appreciated!

2
  • I'd suggest to always include what you have tried to actually solve the problem yourself. At the website of xmlstarlet you can download the documentation. Look for the section of sel which is called 2. Select/Query XML documents. There you find anything you need the query your xml. – Harrys Kavan Nov 18 '20 at 11:37
  • Yes sure :D, will do ! – Abhinav Gupta Nov 19 '20 at 11:09
1

You can print the values of the host XPATHs that do not have a hostname attribute:

$ xmlstarlet sel -t -v '//host[not(hostname)]' -n file.xml
cdn.cutshort.io
www.cutshort.io
blogdata.cutshort.io
cdn.cutshort.io

(//host[not(ip)] would do equally well here).

3
  • thank you so much :D – Abhinav Gupta Nov 18 '20 at 3:03
  • 1
    Or '//host[not(./*)]' to exclude hosts with any descendants. – thanasisp Nov 18 '20 at 3:16
  • @thanasisp thanks - I didn't realize it accepted wildcards - that's good to know – steeldriver Nov 18 '20 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.