xml parsing - Bash: grep pattern to parse command output -


i'm trying parse output of command line tool. outputs xml directly stdou , want parse it.

  • the tool outputs full xml document following:

enter image description here

my goal parse output , the string between <date> tag, since document might contain <date> tags, must check the <date> follows <key>sulastchecktime</key>. (and messy situation new line/spaces there).

currently i'm solving situation following command:

tool... | grep -a1 '<key>sulastchecktime</key>' | grep 'string.$' | sed -e 's,.*<date>\([^<]*\)</date>.*,\1,g' 

it works fine it's messy can see , can't write better? can me making better?

thank you!

ps: since i'm doing in osx, don't have new gnu grepoptions. btw, bash version 3.2.48(1). and... can't afford install other tools parse xml in better way.

maybe this?

$ cat foo.input foo  foo     <key>some key</key>     <date>some date</date> bar  bar     <key>sulastchecktime</key>     <date>2013-08-10t00:27:40z</date> quux  quux 

 

$ awk '/<key>sulastchecktime<\/key>/ { toggle=1 } toggle && /<date>.*<\/date>/ { gsub(/<[^>]*>/, "", $1); print; exit }' foo.input 2013-08-10t00:27:40z 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -