1
<xml>
    <email>[email protected]</email>
    <email>[email protected]</email>
    <email>[email protected]</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. Commented Nov 18, 2020 at 11:37
  • Yes sure :D, will do ! Commented Nov 19, 2020 at 11:09

1 Answer 1

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).

2
  • 1
    Or '//host[not(./*)]' to exclude hosts with any descendants.
    – thanasisp
    Commented Nov 18, 2020 at 3:16
  • @thanasisp thanks - I didn't realize it accepted wildcards - that's good to know Commented Nov 18, 2020 at 12:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .