I'd need the below xml data to be parsed by sed inside my variable and return https://192-168-1-123.385ualfkj3fakedlajkf353029485234.plex.direct:32400
I am using a variable called PLX_IP:
IPADDRESS=$(
curl -b plexcookie.txt "https://plex.tv/pms/resources.xml?includeHttps=1&X-Plex-Token=${PLX_TOK}" | \
sed 's/.*uri=\"\([^\"]*\)\"\slocal.*/\1/g' | \
tail -n 5)
<?xml version="1.0" encoding="UTF-8"?>
<MediaContainer size="6">
<Device name="Test" product="Plex Media Server" productVersion="1.14.1.5490-33830be0b" platform="Android" platformVersion="8.0.0" device="SHIELD Android TV" clientIdentifier="adfafasdfasdfasdfsdf" createdAt="1548647140" lastSeenAt="23424324" provides="server" owned="1" accessToken="asdfasdfadfadsfadsfa" publicAddress="11.111.11.11" httpsRequired="1" synced="0" relay="1" publicAddressMatches="1" presence="1">
<Connection protocol="https" address="192.168.1.123" port="32400" uri="https://192-168-1-123.385ualfkj3fakedlajkf353029485234.plex.direct:32400" local="1"/>
<Connection protocol="https" address="11.111.11.11" port="11111" uri="https://11-111-11-11.385ualfkj3fakedlajkf353029485234.plex.direct:11111" local="0"/>
</Device>
<Device name="Firefox" product="Plex Web" productVersion="3.91.0" platform="Firefox" platformVersion="65.0" device="OSX" clientIdentifier="adfafasdfasdfasdfsdf" createdAt="asdf" lastSeenAt="" provides="client,player,pubsub-player" owned="1" publicAddress="11.111.11.11" publicAddressMatches="1" presence="1" accessToken="asdfasdfadfadsfadsfa">
<Connection protocol="https" address="192.168.1.123" port="32400" uri="https://192-168-1-123.385ualfkj3fakedlajkf353029485234.plex.direct:32400" local="1"/>
<Connection protocol="https" address="11.111.11.11" port="11111" uri="https://11-111-11-11.385ualfkj3fakedlajkf353029485234.plex.direct:11111" local="0"/>
</Device>
</MediaContainer>
This only returns the last 5 lines, but I need just the https://192-168-1-123.385ualfkj3fakedlajkf353029485234.plex.direct:32400
Any help is very much appreciated. I know an xml parser is probably better for this but i'm developing this on an embedded box and can't afford the additional overhead
As oguzismail wrote, this is best done with an xmlparser, but even with simple tools you can solve this problem:
1. grep all interessting lines from xml file
grep 'local="1"' file.xml
this produce:
<Connection protocol="https" address="192.168.1.123" port="32400" uri="https://192-168-1-123.385ualfkj3fakedlajkf353029485234.plex.direct:32400" local="1"/>
<Connection protocol="https" address="192.168.1.123" port="32400" uri="https://192-168-1-123.385ualfkj3fakedlajkf353029485234.plex.direct:32400" local="1"/>
2. extract uri's form this lines
sed "s/.*uri=\"\([^\"]*\)\"\slocal.*/\1/g"
this produce:
https://192-168-1-123.385ualfkj3fakedlajkf353029485234.plex.direct:32400
https://192-168-1-123.385ualfkj3fakedlajkf353029485234.plex.direct:32400
3. the uri's are indentical and you can take one of it (e.g. the last)
tail -1
this produce
https://192-168-1-123.385ualfkj3fakedlajkf353029485234.plex.direct:32400
4. concatenate all parts together
grep 'local="1"' file.xml | sed "s/.*uri=\"\([^\"]*\)\"\slocal.*/\1/g" | tail -1
And you get what you wish!