Computing.Net > Forums > Unix > Extract strings from big xml file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Extract strings from big xml file

Reply to Message Icon

Name: ayhanne
Date: November 14, 2007 at 09:24:51 Pacific
OS: Unix
CPU/Ram: sun
Product: Sun Netra-240
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: lankrypt0
Date: November 15, 2007 at 07:37:23 Pacific
Reply:

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


0

Response Number 2
Name: FishMonger
Date: November 15, 2007 at 17:16:37 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


New Tower case Problem Help with shell script



Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Extract strings from big xml file

Extracting data from file using sed www.computing.net/answers/unix/extracting-data-from-file-using-sed/7660.html

Extracting lines from file using sh www.computing.net/answers/unix/extracting-lines-from-file-using-sh/7664.html

Extracting information from a file www.computing.net/answers/unix/extracting-information-from-a-file/7770.html