0

I using this command for below xml file.

xmlstarlet sel -t -v '//channel[protected = "True"]/playlist_url' -nl file.xml

and result;

host.net/aa/vodpr
host.net/aa/vodpr/con_tv_r.php

Can I choose a line in result? Example only last line or 2. line?

<?xml version="1.0" encoding="UTF-8" ?>
<items>   

<channel>
<title><![CDATA[*** variable text ***]]></title>
<playlist_url><![CDATA[http://host.net/aa/15_info/]]></playlist_url>
</channel>

<channel>       
<title><![CDATA[*** variable text ***]]></title>
<playlist_url><![CDATA[http://host.net/aa/16_info/]]></playlist_url>
</channel>

<channel>      
<title><![CDATA[*** variable text ***]]></title>
<playlist_url><![CDATA[http://host.net/aa/vodpr/]]></playlist_url>  
<protected>True</protected> 
</channel>

<channel>
<title><![CDATA[*** variable text ***]]></title>
<playlist_url><![CDATA[http://host.net/aa/vodpr/con_tv_r.php]]></playlist_url>
<protected>True</protected> 
</channel>

</items>
1

To select the playlist_url value from the channel node by position in the list, use position() and compare with some positive integer:

$ xmlstarlet sel -t -v '//channel[position() = 4]/playlist_url' -nl file.xml
http://host.net/aa/vodpr/con_tv_r.php

(in this case, you could use just [4] in place of [position() = 4]).

$ xmlstarlet sel -t -v '//channel[position() > 2]/playlist_url' -nl file.xml
http://host.net/aa/vodpr/
http://host.net/aa/vodpr/con_tv_r.php

To get the last two from a list of unknown length, combine the test with last():

$ xmlstarlet sel -t -v '//channel[position() >= last() - 1]/playlist_url' -nl file.xml
http://host.net/aa/vodpr/
http://host.net/aa/vodpr/con_tv_r.php

To get the second "protected" URL:

$ xmlstarlet sel -t -v '//channel[protected = "True"][2]/playlist_url' -nl file.xml
http://host.net/aa/vodpr/con_tv_r.php

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.