sed command to extract from xml
I'm using my mac terminal to do a script, it basically does:
wget http://p2.edms-pr.ccomrcdn.com/player/player_dispatcher.html?section=radio&action=listen_live
This file returns an XML which I can save to txt or XML, I'm saving it as "url.xml"
<PlayerContent>
<ListenLiveInitialize>
<StreamInfo>
<stream id="4694" primary_location="rtmp://cp58082.live.edgefcs.net/live/COR_5103_OR@s5137?auth=daEaIcRcbb.afahbOdwbWdjdYcEdYaOaDdc-bn7nM7-4q-PN0X1_3nqDHom4EBvmEuwr&aifp=1234&CHANNELID=4694&CPROG=_&MARKET=PREMIERE&REQUESTOR=EDMS-PR&SERVER_NAME=p2.edms-pr.ccomrcdn.com&SITE_ID=13293&STATION_ID=EDMS-PR&MNM=_&TYPEOFPLAY=0" backup_location=""/>
</StreamInfo>
<JustPlayed/>
I want to used SED to return the AUTH code inside "primary_location". So basically I want to store
daEaIcRcbb.afahbOdwbWdjdYcEdYaOaDdc-bn7nM7-4q-PN0X1_3nqDHom4EBvmEuwr
o开发者_运维技巧n a variable.
I found this online but it doesn't seem to be working.
sed -n 's/.*\(auth=......................................... ...........................\).*/\1/p' url.xml
Try
sed -n 's|^<stream.*auth\=\(.*\)\&ai.*|\1|p' url.xml
which reads the file and matches the line up to the =
before the auth code, stores everything from there up to the &
in &ai
as \1
which is then substituted for the whole pattern space.
You have a stray space () in the middle of your
.
s!
This is neater and will output auth=
with the value (it looks like it's a string of alphanumerics with hyphens and underscores):
% grep -o 'auth=[[:alnum:]_-]\+' url.xml
You could even use it like so:
% eval $(grep -o 'auth=[[:alnum:]_-]\+' url.xml)
% echo ${auth}
daEaIcRcbb.afahbOdwbWdjdYcEdYaOaDdc-bn7nM7-4q-PN0X1_3nqDHom4EBvmEuwr
Works on OSX.
精彩评论