Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I have big xml files wherein I need to extract and print values between <status> </status>, <date> </date> and <time> </time>. Can you please help me create a script to do this or can I just use sed command? Please see sample of xml file below. Thanks in advance! I hope you can help me.<?xml version="1.0"?><message><cdr version="1.0"><appid>testbed</appid><threadid>6</threadid><origin>node1</origin><date>20071009
</date><time>12:45:36</time><chdate>20071009</chdate><chtime>12:45:43</chtime><status>201</status><type>103</type><calling>644</call
ing><cparty>xxxxxxx</cparty><accnum>xxxxxx</accnum><debirate1>0.0</debirate1><cos>-1</cos><strtbal>0.0</strtbal><freesms>0</
freesms><tuc>0</tuc><fandftype></fandftype></cdr></message>expected output:
date time status
20071009 12:45:36 201
.... and so on

This should work, its a one liner, the site will break it up, so just copy/paste onto one line:
sed -e "s/></>\n</g" < tfile|grep -e '<date>' -e '<time>' -e '<status>'|sed -e 's/<date>/Date\nTime\nStatus\n/g'|sed -e 's/<\/.*>//g' -e 's/<.*>//g'|tr "\n" " "|sed -e "s/Status /Status\n/g" -e "s/ Date/\nDate/g

Here's a Perl 1 liner (which I broke across several lines for readability) that is about half the length of the piped sed calls.
perl -MXML::Simple -e '
$xml = XMLin("data.xml");
print "date Time Status\n";
print "$xml->{'cdr'}{$_} " for qw(date time status)'Your xml file needs to be properly formated for this command to do its work.

![]() |
New Tower case Problem
|
Help with shell script
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |