Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Xml extraction

Status
Not open for further replies.

nishantu

Programmer
May 30, 2003
7
0
0
IN
I have an xml file (given below) which lists out installs, changes and removes for each product. The xml also provides totals at the end.
I need to extract the totals (system_change_msg/totals/total_installs, system_change_msg/totals/total_changes and system_change_msg/totals/total_changes) using awk... anybody has any idea on how to do this?

Thanks
Nishant

<system_change_msg
transaction="1"
changed_by="SW$ADMIN"
device_id="0"
object_name="device_counts"
batch="1"
changed_by_timestamp="05/19/2005 01:45:09"
event_type="install">
<product_abc>
<total_installs>13</total_installs>
<total_changes>2</total_changes>
<total_removes>3</total_removes>
</product_abc>
<product_xyz>
<total_installs>4</total_installs>
<total_changes>51</total_changes>
<total_removes>42</total_removes>
</product_xyz>
<product_pqr>
<total_installs>0</total_installs>
<total_changes>300</total_changes>
<total_removes>22</total_removes>
</product_pqr>
<totals>
<total_installs>17</total_installs>
<total_changes>353</total_changes>
<total_removes>67</total_removes>
</totals>
</system_change_msg>
 
try
Code:
/<system_change_msg/{flg=1;;next}
/^<totals>/ && flg==1{flg++;;next}
/<\/system_change_msg/{flg=0;;next}
/^<\/totals>/ && flg==2{flg--;;next}
/^<total_installs>/ && flg==2{
  gsub(/<[^>]*>/,"")
  print "Total installs " $0
}
/^<total_changes>/ && flg==2{
  gsub(/<[^>]*>/,"")
  print "Total changes " $0
}
/^<total_removes>/ && flg==2{
  gsub(/<[^>]*>/,"")
  print "Total removes " $0
}

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top