string - Parsing xml data into nested list bash -


i'm working on plex geeklet, , have string of added tv shows.

show_data=$(curl --silent "http://localhost:32400/library/sections/3/recentlyadded?x-plex-container-start=0&x-plex-container-size=10") 

this example of data:

<?xml version="1.0" encoding="utf-8"?> <mediacontainer size="10" totalsize="50" allowsync="1" art="/:/resources/show-fanart.jpg" identifier="com.plexapp.plugins.library" librarysectionid="3" librarysectiontitle="tv shows" librarysectionuuid="600cd0c5-fd4b-460a-846b-e4bad1ecdf4a" mediatagprefix="/system/bundle/media/flags/" mediatagversion="1402960845" mixedparents="1" nocache="1" offset="0" thumb="/:/resources/show.png" title1="tv shows" title2="recently added" viewgroup="episode" viewmode="65592"> <video ratingkey="588" key="/library/metadata/588" parentratingkey="587" grandparentratingkey="586" type="episode" title="pilot" grandparentkey="/library/metadata/586" parentkey="/library/metadata/587" grandparenttitle="community" contentrating="tv-pg" summary="fast-talking lawyer jeff winger (joel mchale) enrolls @ greendale community college after state bar discovered illegitimate degree , threatened suspend license. when jeff pretends spanish tutor close classmate britta (gillian jacobs), winds entire study group of students looking help. pierce (chevy chase), abed (danny pudi), shirley (yvette nicole brown), annie (alison brie), troy (donald glover), , britta comprise band of misfits jeff never asked for, may end needing when realizes connection greendale professor ian duncan (john oliver) won&apos;t pay off hoped." index="1" parentindex="1" rating="7.4000000953674299" year="2009" thumb="/library/metadata/588/thumb/1403755683" art="/library/metadata/586/art/1403755684" parentthumb="/library/metadata/587/thumb/1403755684" grandparentthumb="/library/metadata/586/thumb/1403755684" grandparenttheme="/library/metadata/586/theme/1403755684" duration="1525134" originallyavailableat="2009-09-17" addedat="1403755618" updatedat="1403755683"> <media videoresolution="480" id="479" duration="1525134" bitrate="2509" width="854" height="480" aspectratio="1.78" audiochannels="2" audiocodec="aac" videocodec="h264" container="mp4" videoframerate="24p" optimizedforstreaming="0" has64bitoffsets="0"> <part id="522" key="/library/parts/522/file.mp4" duration="1525134" file="/users/joe/videos/tv shows/community/season 1/01 pilot.mp4" size="478232014" container="mp4" has64bitoffsets="0" optimizedforstreaming="0" /> </media> <writer tag="dan harmon" /> <director tag="anthony russo" /> <director tag="joe russo" /> </video> <video ratingkey="589" key="/library/metadata/589" parentratingkey="587" grandparentratingkey="586" type="episode" title="spanish 101" grandparentkey="/library/metadata/586" parentkey="/library/metadata/587" grandparenttitle="community" contentrating="tv-pg" summary="jeff&apos;s (joel mchale) efforts win on britta (gillian jacobs) backfire, , finds himself paired pierce (chevy chase) spanish class project. 2 give teacher señor chang (ken jeong) presentation of lifetime. meanwhile, inspired britta&apos;s awareness of social issues, annie (alison brie) , shirley (yvette nicole brown) stage protest on greendale&apos;s campus." index="2" parentindex="1" rating="7.4000000953674299" year="2009" thumb="/library/metadata/589/thumb/1403755684" art="/library/metadata/586/art/1403755684" parentthumb="/library/metadata/587/thumb/1403755684" grandparentthumb="/library/metadata/586/thumb/1403755684" grandparenttheme="/library/metadata/586/theme/1403755684" duration="1278352" originallyavailableat="2009-09-24" addedat="1403755618" updatedat="1403755684"> <media videoresolution="480" id="480" duration="1278352" bitrate="2253" width="854" height="480" aspectratio="1.78" audiochannels="2" audiocodec="aac" videocodec="h264" container="mp4" videoframerate="24p" optimizedforstreaming="0" has64bitoffsets="0"> <part id="523" key="/library/parts/523/file.mp4" duration="1278352" file="/users/joe/videos/tv shows/community/season 1/02 spanish 101.mp4" size="359953984" container="mp4" has64bitoffsets="0" optimizedforstreaming="0" /> </media> <writer tag="dan harmon" /> <director tag="joe russo" /> </video> <video ratingkey="591" key="/library/metadata/591" parentratingkey="587" grandparentratingkey="586" type="episode" title="introduction statistics" grandparentkey="/library/metadata/586" parentkey="/library/metadata/587" grandparenttitle="community" contentrating="tv-pg" summary="it&apos;s halloween @ greendale, , jeff (joel mchale) has hots 1 of teachers (lauren stamile) , gets dating advice señor chang (ken jeong). meanwhile annie (alison brie) throws &quot;&quot;dia de los muertos&quot; party credit." index="7" parentindex="1" rating="7.9000000953674299" year="2009" thumb="/library/metadata/591/thumb/1403755686" art="/library/metadata/586/art/1403755684" parentthumb="/library/metadata/587/thumb/1403755684" grandparentthumb="/library/metadata/586/thumb/1403755684" grandparenttheme="/library/metadata/586/theme/1403755684" duration="1276610" originallyavailableat="2009-10-29" addedat="1403755618" updatedat="1403755686"> <media videoresolution="480" id="482" duration="1276610" bitrate="2258" width="854" height="480" aspectratio="1.78" audiochannels="2" audiocodec="aac" videocodec="h264" container="mp4" videoframerate="24p" optimizedforstreaming="0" has64bitoffsets="0"> <part id="525" key="/library/parts/525/file.mp4" duration="1276610" file="/users/joe/videos/tv shows/community/season 1/07 introduction statistics.mp4" size="360268838" container="mp4" has64bitoffsets="0" optimizedforstreaming="0" /> </media> <writer tag="jon pollack" /> <writer tag="tim hobert" /> <director tag="justin lin" /> </video> <video ratingkey="592" key="/library/metadata/592" parentratingkey="587" grandparentratingkey="586" type="episode" title="home economics" grandparentkey="/library/metadata/586" parentkey="/library/metadata/587" grandparenttitle="community" contentrating="tv-pg" summary="britta (gillian jacobs) tries rid jeff (joel mchale) of materialistic ways. meanwhile pierce (chevy chase) joins rock band on campus, , annie (alison brie) grudgingly helps troy (donald glover) plan date girl." index="8" parentindex="1" rating="7.5999999046325701" year="2009" thumb="/library/metadata/592/thumb/1403755686" art="/library/metadata/586/art/1403755684" parentthumb="/library/metadata/587/thumb/1403755684" grandparentthumb="/library/metadata/586/thumb/1403755684" grandparenttheme="/library/metadata/586/theme/1403755684" duration="1275844" originallyavailableat="2009-11-05" addedat="1403755618" updatedat="1403755686"> <media videoresolution="480" id="483" duration="1275844" bitrate="2340" width="854" height="480" aspectratio="1.78" audiochannels="2" audiocodec="aac" videocodec="h264" container="mp4" videoframerate="24p" optimizedforstreaming="0" has64bitoffsets="0"> <part id="526" key="/library/parts/526/file.mp4" duration="1275844" file="/users/joe/videos/tv shows/community/season 1/08 home economics.mp4" size="373156573" container="mp4" has64bitoffsets="0" optimizedforstreaming="0" /> </media> <writer tag="lauren pomerantz" /> <director tag="anthony russo" /> </video> <video ratingkey="593" key="/library/metadata/593" parentratingkey="587" grandparentratingkey="586" type="episode" title="comparative religion" grandparentkey="/library/metadata/586" parentkey="/library/metadata/587" grandparenttitle="community" contentrating="tv-pg" summary="shirley (yvette nicole brown) tries in christmas spirit, jeff (joel mchale) threatens holiday cheer when decides fight school bully (guest star anthony michael hall)." index="12" parentindex="1" rating="7.8000001907348597" year="2009" thumb="/library/metadata/593/thumb/1403755688" art="/library/metadata/586/art/1403755684" parentthumb="/library/metadata/587/thumb/1403755684" grandparentthumb="/library/metadata/586/thumb/1403755684" grandparenttheme="/library/metadata/586/theme/1403755684" duration="1276355" originallyavailableat="2009-12-10" addedat="1403755618" updatedat="1403755688"> <media videoresolution="480" id="484" duration="1276355" bitrate="2446" width="854" height="480" aspectratio="1.78" audiochannels="2" audiocodec="aac" videocodec="h264" container="mp4" videoframerate="24p" optimizedforstreaming="0" has64bitoffsets="0"> <part id="527" key="/library/parts/527/file.mp4" duration="1276355" file="/users/joe/videos/tv shows/community/season 1/12 comparative religion.mp4" size="390216047" container="mp4" has64bitoffsets="0" optimizedforstreaming="0" /> </media> <writer tag="liz cackowski" /> <director tag="adam davidson" /> </video> <video ratingkey="594" key="/library/metadata/594" parentratingkey="587" grandparentratingkey="586" type="episode" title="investigative journalism" grandparentkey="/library/metadata/586" parentkey="/library/metadata/587" grandparenttitle="community" contentrating="tv-pg" summary="everyone&apos;s vibe thrown off when unwanted outsider tries join study group. meanwhile, jeff (joel mchale) becomes new editor of greendale&apos;s school newspaper , appoints annie (alison brie) ace reporter." index="13" parentindex="1" rating="7.5" year="2010" thumb="/library/metadata/594/thumb/1403755689" art="/library/metadata/586/art/1403755684" parentthumb="/library/metadata/587/thumb/1403755684" grandparentthumb="/library/metadata/586/thumb/1403755684" grandparenttheme="/library/metadata/586/theme/1403755684" duration="1269923" originallyavailableat="2010-01-14" addedat="1403755618" updatedat="1403755689"> <media videoresolution="480" id="485" duration="1269923" bitrate="1998" width="854" height="480" aspectratio="1.78" audiochannels="2" audiocodec="aac" videocodec="h264" container="mp4" videoframerate="24p" optimizedforstreaming="0" has64bitoffsets="0"> <part id="528" key="/library/parts/528/file.mp4" duration="1269923" file="/users/joe/videos/tv shows/community/season 1/13 investigative journalism.mp4" size="317146865" container="mp4" has64bitoffsets="0" optimizedforstreaming="0" /> </media> <writer tag="jon pollack" /> <writer tag="tim hobert" /> <director tag="joe russo" /> </video> <video ratingkey="595" key="/library/metadata/595" parentratingkey="587" grandparentratingkey="586" type="episode" title="romantic expressionism" grandparentkey="/library/metadata/586" parentkey="/library/metadata/587" grandparenttitle="community" contentrating="tv-pg" summary="britta (gillian jacobs) , jeff (joel mchale) stage intervention when annie (alison brie) gets cozy vaughn (eric christian olsen). meanwhile pierce (chevy chase) struggles prove wit when crashes abed (danny pudi) , troy’s (donald glover) movie night." index="15" parentindex="1" rating="7.9000000953674299" year="2010" thumb="/library/metadata/595/thumb/1403755689" art="/library/metadata/586/art/1403755684" parentthumb="/library/metadata/587/thumb/1403755684" grandparentthumb="/library/metadata/586/thumb/1403755684" grandparenttheme="/library/metadata/586/theme/1403755684" duration="1274799" originallyavailableat="2010-02-04" addedat="1403755618" updatedat="1403755689"> <media videoresolution="480" id="486" duration="1274799" bitrate="2059" width="854" height="480" aspectratio="1.78" audiochannels="2" audiocodec="aac" videocodec="h264" container="mp4" videoframerate="24p" optimizedforstreaming="0" has64bitoffsets="0"> <part id="529" key="/library/parts/529/file.mp4" duration="1274799" file="/users/joe/videos/tv shows/community/season 1/15 romantic expressionism.mp4" size="328027632" container="mp4" has64bitoffsets="0" optimizedforstreaming="0" /> </media> <writer tag="andrew guest" /> <director tag="joe russo" /> </video> <video ratingkey="596" key="/library/metadata/596" parentratingkey="587" grandparentratingkey="586" type="episode" title="communication studies" grandparentkey="/library/metadata/586" parentkey="/library/metadata/587" grandparenttitle="community" contentrating="tv-pg" summary="when britta (gillian jacobs) drunk dials jeff (joel mchale) things awkward between them , jeff attempts repair relationship. meanwhile, annie (alison brie) , shirley (yvette nicole brown) conspire humiliate señor chang (ken jeong)." index="16" parentindex="1" rating="7.8000001907348597" year="2010" thumb="/library/metadata/596/thumb/1403755691" art="/library/metadata/586/art/1403755684" parentthumb="/library/metadata/587/thumb/1403755684" grandparentthumb="/library/metadata/586/thumb/1403755684" grandparenttheme="/library/metadata/586/theme/1403755684" duration="1267206" originallyavailableat="2010-02-11" addedat="1403755618" updatedat="1403755691"> <media videoresolution="480" id="487" duration="1267206" bitrate="2278" width="854" height="480" aspectratio="1.78" audiochannels="2" audiocodec="aac" videocodec="h264" container="mp4" videoframerate="24p" optimizedforstreaming="0" has64bitoffsets="0"> <part id="530" key="/library/parts/530/file.mp4" duration="1267206" file="/users/joe/videos/tv shows/community/season 1/16 communication studies.mp4" size="360777073" container="mp4" has64bitoffsets="0" optimizedforstreaming="0" /> </media> <writer tag="chris mckenna" /> <director tag="adam davidson" /> </video> <video ratingkey="597" key="/library/metadata/597" parentratingkey="587" grandparentratingkey="586" type="episode" title="modern warfare" grandparentkey="/library/metadata/586" parentkey="/library/metadata/587" grandparenttitle="community" contentrating="tv-pg" summary="jeff , britta&apos;s sexual tension heats ? sexual tension between jeff (joel mchale) , britta (gillian jacobs) becomes hot topic among study group. meanwhile, starts out simple contest chance @ class registration turns peaceful campus of greendale community college all-out war zone. friendships tested, 1 student can victorious." index="23" parentindex="1" rating="8.5" year="2010" thumb="/library/metadata/597/thumb/1403755692" art="/library/metadata/586/art/1403755684" parentthumb="/library/metadata/587/thumb/1403755684" grandparentthumb="/library/metadata/586/thumb/1403755684" grandparenttheme="/library/metadata/586/theme/1403755684" duration="1260333" originallyavailableat="2010-05-06" addedat="1403755618" updatedat="1403755692"> <media videoresolution="480" id="488" duration="1260333" bitrate="2233" width="854" height="480" aspectratio="1.78" audiochannels="2" audiocodec="aac" videocodec="h264" container="mp4" videoframerate="24p" optimizedforstreaming="0" has64bitoffsets="0"> <part id="531" key="/library/parts/531/file.mp4" duration="1260333" file="/users/joe/videos/tv shows/community/season 1/23 modern warfare.mp4" size="351822199" container="mp4" has64bitoffsets="0" optimizedforstreaming="0" /> </media> <writer tag="emily cutler" /> <director tag="justin lin" /> </video> <video ratingkey="590" key="/library/metadata/590" parentratingkey="587" grandparentratingkey="586" type="episode" title="advanced criminal law" grandparentkey="/library/metadata/586" parentkey="/library/metadata/587" grandparenttitle="community" contentrating="tv-pg" summary="señor chang (ken jeong) invokes inquisition , trial when 1 of gang cheats on exam. annie (alison brie) enlists pierce (chevy chase) compose greendale&apos;s new school song, , troy (donald glover) educates abed (danny pudi) on art of joking." index="5" parentindex="1" rating="7.8000001907348597" year="2009" thumb="/library/metadata/590/thumb/1403755683" art="/library/metadata/586/art/1403755684" parentthumb="/library/metadata/587/thumb/1403755684" grandparentthumb="/library/metadata/586/thumb/1403755684" grandparenttheme="/library/metadata/586/theme/1403755684" originallyavailableat="2009-10-15" addedat="1403755618" updatedat="1403755683"> <media id="481" container=""> <part id="524" key="/library/parts/524/file.mp4" file="/users/joe/videos/tv shows/community/season 1/05 advanced criminal law.mp4" size="48" /> </media> <writer tag="andrew guest" /> <director tag="joe russo" /> </video> </mediacontainer> 

i'm trying orders list this

[ [show_title_1, episode_title_1], [show_title_2, episode_title_2], ... [show_title_10, episode_title_10] ] 

so can print (for example)

community: pilot
arrested development: mother, car

i've been able them separate newline-delimited strings so:

show_names=$("$show_data" | grep -o 'grandparenttitle="\([^"]*\)"' | sed -e 's/grandparenttitle="//' -e 's/"//' | perl -mhtml::entities -ne 'print decode_entities($_)')) show_titles=$("$show_data" | grep -o 'title="\([^"]*\)"' | sed -e 's/title="//' -e 's/"//' | perl -mhtml::entities -ne 'print decode_entities($_)')) 

so possible convert them both lists, , use loop construct new list containing nested lists of values? i've hit wall here.

if rid of grep , sed, etc.. , rely on pattern matching. guessed @ exact fields wanted, can update code needed. used data file test with, work. script takes 1 input, being filename downloaded .xml file:

#!/bin/bash  test -r "$1" || { echo "error: invalid input, usage: ${0//*\//} filename.xml"; exit 1; }  let idx=0  while read line || test -n "$line";      if test "${line:0:2}" == '<v';          tmp=${line##* title=}         title=${tmp%% grandparentkey*}          tmp=${line##*grandparenttitle=}         gptitle=${tmp%% contentrating*}          if test "$idx" -lt "1" ;             let idx=1             echo -n "[ [ $title, $gptitle ]"         else             echo -n ", [ $title, $gptitle ]"         fi     fi  done <"$1"  echo " ]"  exit 0 

the following read values array allow later processing. can add additional arrays like, can read entire file array if (but re-reading if disk simple enough well) output same above (it illustration):

let idx=0 declare -a title declare -a gptitle declare -a allvideo  while read line || test -n "$line";      if test "${line:0:2}" == '<v';          allvideo+=( "$line" )          tmp=${line##* title=}         title+=( "${tmp%% grandparentkey*}" )          tmp=${line##*grandparenttitle=}         gptitle+=( "${tmp%% contentrating*}" )      fi  done <"$1"  # output original 2 variables ((i=0; i<${#title[@]}; i++));          if test "$i" -eq 0 ;             echo -n "[ [ ${title[$i]}, ${gptitle[$i]} ]"         else             echo -n ", [ ${title[$i]}, ${gptitle[$i]} ]"         fi  done  echo " ]"  oldifs=$ifs ifs=$'\n'  # set internal field separator break on newlines  # output entire file allvideo array in ${allvideo[@]};     echo "$i" done  ifs=$oldifs 

output:

[ [ "pilot", "community" ], [ "spanish 101", "community" ], \ [ "introduction statistics", "community" ], [ "home economics", "community" ], \ [ "comparative religion", "community" ], [ "investigative journalism", "community" ], \ [ "romantic expressionism", "community" ], [ "communication studies", "community" ], \ [ "modern warfare", "community" ], [ "advanced criminal law", "community" ] ] **the dump of original file omitted brevity. 

let me know if have additional questions.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -