我需要以下XML数据由我的变量内部解析并返回https://192-168-1-123.385ualfkj3fakedlajkf353029485234.plex.direct:32400
我正在使用一个称为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>
这仅返回最后5行,但我只需要https://192-168-1-123.385ualfkj3fakedlajkf353029485234.plex.direct:32400
任何帮助都非常感谢。我知道XML解析器可能会更好,但是我正在嵌入式盒子上开发它,并且负担不起额外的开销
as oguzismail 写道,最好使用 xmlparser 来完成,但是即使使用简单的工具,您也可以解决此问题:p> 1。从XML文件
中grep所有grep 'local="1"' file.xml
这个产物:
<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。提取URI的形式此行
sed "s/.*uri="([^"]*)"slocal.*/1/g"
这个产物:
https://192-168-1-123.385ualfkj3fakedlajkf353029485234.plex.direct:32400
https://192-168-1-123.385ualfkj3fakedlajkf353029485234.plex.direct:32400
3。URI是固定的,您可以将其中之一(例如最后一个)
tail -1
这个产生
https://192-168-1-123.385ualfkj3fakedlajkf353029485234.plex.direct:32400
4。将所有部分加在一起
grep 'local="1"' file.xml | sed "s/.*uri="([^"]*)"slocal.*/1/g" | tail -1
您得到了想要的东西!