Script to group XML entries?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to group XML entries?
# 1  
Old 02-13-2013
Script to group XML entries?

Hello all, If I wanted to take an xml file as INPUT to a script to pull information from it how do you group things together? Here is what I am trying to do:

In the xml file you have:

<Group version="1.0">
<ID>85330</ID>
<DeviceIDList rootURL="devices" relatesURL="relatesto/devices">
<ID>85847</ID>
<ID>85211</ID>
<ID>21051</ID>
</DeviceIDList>
<Item version="1.0">
<Name>Citrix NetScaler Devices</Name>
</Item>
</Group>

This is a small sample of just one <Group>... out of 100's. I am looking to look at every group and get output like this:

Group ID: 85330
DeviceID List
85847
85211
21051
Group Name: Citrix NetScaler Devices ...

And, is there a way to parse from another XML file the DeviceIDList and append the output of the above output so that the final output looks like this:

Group ID: 85330
DeviceID List
85847 - losa-5-bldg1
85211 - losa-9-blg04
21051 - rtf-core-5
Group Name: Citrix NetScaler Devices

Last edited by dlundwall; 02-13-2013 at 09:44 AM.. Reason: Added info
# 2  
Old 02-13-2013
Yes, it is generally possible to do so using a transformational language such XSLT but without seeing the full XML files, I cannot say for certain.
# 3  
Old 02-13-2013
Understood. Here is the complete xml code:

Code:
<?xml version="1.0"?>
<GroupList>
 <Group version="1.0.0">
  <ID>39060</ID>
  <IsAlso>
   <IsA name="SyncableGroup" rootURL="syncablegroup"/>
   <IsA name="Syncable" rootURL="syncable"/>
  </IsAlso>
  <Syncable version="1.0.0"/>
  <Item version="1.0.0">
   <CreateTime>Mon Nov 19 16:20:34 PST 2012</CreateTime>
   <Name>San Jose, CA (SJC)</Name>
  </Item>
 </Group>
 <Group version="1.0.0">
  <ID>244</ID>
  <IsAlso>
   <IsA name="SyncableGroup" rootURL="syncablegroup"/>
   <IsA name="Syncable" rootURL="syncable"/>
  </IsAlso>
  <Syncable version="1.0.0"/>
  <Item version="1.0.0">
   <CreateTime>Mon Nov 12 16:47:02 PST 2012</CreateTime>
   <Name>Interfaces</Name>
   <Description>Includes interfaces from all data sources.</Description>
  </Item>
 </Group>
 <Group version="1.0.0">
  <ID>40151</ID>
  <RelatesTo>
   <MonitoringProfileIDList relatesURL="relatesto/monitoringprofiles" rootURL="monitoringprofiles">
    <ID>48717</ID>
   </MonitoringProfileIDList>
   <DeviceIDList relatesURL="relatesto/devices" rootURL="devices">
    <ID>56717</ID>
    <ID>56718</ID>
    <ID>56719</ID>
    <ID>56720</ID>
    <ID>56713</ID>
    <ID>56714</ID>
    <ID>56715</ID>
    <ID>56716</ID>
    <ID>56709</ID>
    <ID>56710</ID>
    <ID>39905</ID>
    <ID>39898</ID>
    <ID>39897</ID>
    <ID>39900</ID>
    <ID>39899</ID>
    <ID>39902</ID>
    <ID>56722</ID>
    <ID>39901</ID>
    <ID>56721</ID>
    <ID>39903</ID>
    <ID>56723</ID>
   </DeviceIDList>
  </RelatesTo>
  <IsAlso>
   <IsA name="MonitoredGroup" rootURL="monitoredgroups"/>
   <IsA name="SyncableGroup" rootURL="syncablegroup"/>
   <IsA name="Syncable" rootURL="syncable"/>
  </IsAlso>
  <Syncable version="1.0.0"/>
  <Item version="1.0.0">
   <CreateTime>Mon Nov 26 16:03:53 PST 2012</CreateTime>
   <Name>Voice Call Center</Name>
  </Item>
  <MonitoredGroup version="0.0.0"/>
 </Group>
 <Group version="1.0.0">
  <ID>250</ID>
  <RelatesTo>
   <DeviceIDList relatesURL="relatesto/devices" rootURL="devices">
    <ID>18797</ID>
    <ID>18791</ID>
    <ID>18818</ID>
    <ID>18817</ID>
    <ID>18875</ID>
    <ID>18816</ID>
    <ID>18907</ID>
    <ID>18908</ID>
    <ID>18909</ID>
    <ID>18910</ID>
    <ID>18911</ID>
    <ID>18912</ID>
    <ID>18685</ID>
    <ID>18684</ID>
    <ID>18782</ID>
   </DeviceIDList>
  </RelatesTo>
  <IsAlso>
   <IsA name="SyncableGroup" rootURL="syncablegroup"/>
   <IsA name="Syncable" rootURL="syncable"/>
  </IsAlso>
  <Syncable version="1.0.0"/>
  <Item version="1.0.0">
   <CreateTime>Mon Nov 12 16:47:02 PST 2012</CreateTime>
   <Name>Pingable Devices</Name>
   <Description>This group contains all the pingable devices reported by each data source.</Description>
  </Item>
  <MonitoredGroup version="0.0.0">
   <RelatesTo>
    <DeviceIDList relatesURL="relatesto/devices" rootURL="devices">
     <ID>18797</ID>
     <ID>18791</ID>
     <ID>18818</ID>
     <ID>18817</ID>
     <ID>18875</ID>
     <ID>18816</ID>
     <ID>18907</ID>
     <ID>18908</ID>
     <ID>18909</ID>
     <ID>18910</ID>
     <ID>18911</ID>
     <ID>18912</ID>
     <ID>18685</ID>
     <ID>18684</ID>
     <ID>18782</ID>
    </DeviceIDList>
   </RelatesTo>
  </MonitoredGroup>
 </Group>
 <Group version="1.0.0">
  <ID>249</ID>
  <IsAlso>
   <IsA name="SyncableGroup" rootURL="syncablegroup"/>
   <IsA name="Syncable" rootURL="syncable"/>
  </IsAlso>
  <Syncable version="1.0.0"/>
  <Item version="1.0.0">
   <CreateTime>Mon Nov 12 16:47:02 PST 2012</CreateTime>
   <Name>Device Components</Name>
   <Description>This group contains all the device components reported by each data source.</Description>
  </Item>
 </Group>
 <Group version="1.0.0">
  <ID>39072</ID>
  <IsAlso>
   <IsA name="SyncableGroup" rootURL="syncablegroup"/>
   <IsA name="Syncable" rootURL="syncable"/>
  </IsAlso>
  <Syncable version="1.0.0"/>
  <Item version="1.0.0">
   <CreateTime>Mon Nov 19 16:25:36 PST 2012</CreateTime>
   <Name>Cairo, Egypt (CAI)</Name>
  </Item>
 </Group>
 <Group version="1.0.0">
  <ID>39786</ID>
  <RelatesTo>
   <MonitoringProfileIDList relatesURL="relatesto/monitoringprofiles" rootURL="monitoringprofiles">
    <ID>48717</ID>
   </MonitoringProfileIDList>
   <DeviceIDList relatesURL="relatesto/devices" rootURL="devices">
    <ID>18913</ID>
    <ID>18680</ID>
    <ID>18815</ID>
    <ID>18914</ID>
    <ID>18814</ID>
    <ID>18811</ID>
    <ID>18809</ID>
    <ID>18810</ID>
    <ID>18808</ID>
    <ID>85847</ID>
    <ID>18681</ID>
    <ID>39903</ID>
   </DeviceIDList>
  </RelatesTo>
  <IsAlso>
   <IsA name="MonitoredGroup" rootURL="monitoredgroups"/>
   <IsA name="SyncableGroup" rootURL="syncablegroup"/>
   <IsA name="Syncable" rootURL="syncable"/>
  </IsAlso>
  <Syncable version="1.0.0"/>
  <Item version="1.0.0">
   <CreateTime>Mon Nov 26 10:51:43 PST 2012</CreateTime>
   <Name>Las Vegas SERVERS</Name>
   <Description>All servers found in Las Vegas</Description>
  </Item>
  <MonitoredGroup version="0.0.0"/>
 </Group>
 <Group version="1.0.0">
  <ID>227</ID>
  <IsAlso>
   <IsA name="SyncableGroup" rootURL="syncablegroup"/>
   <IsA name="Syncable" rootURL="syncable"/>
  </IsAlso>
  <Syncable version="1.0.0"/>
  <Item version="1.0.0">
   <CreateTime>Mon Nov 12 16:47:02 PST 2012</CreateTime>
   <Name>All Groups</Name>
   <Description>Includes every group and item type defined within the performance center.</Description>
  </Item>
 </Group>

So, in the above example xml file at the end you have Group ID 39786 which references to "Las Vegas SERVERS". The DeviceIDList are the ID's that correspond to the devices in ANOTHER xml file. So in the case above, I am trying to get output like:

Group Info: Las Vegas SERVERS (39786)
Devices in group:
lv-svr01-fs1 (18913)
lv-svr01-fs2 (18680)
lv-svr02-nx1 (18815)
lv-svr03-nx1 (18914)
...
Total in group: 12
# 4  
Old 02-13-2013
Use an awk program to parse your xml. E.g:
Code:
awk -F'[<>]' -v V=39786 ' /<ID>/ && $3 == V {
                        f = 1;
                        next;
                } /<DeviceIDList/ && f {
                        i = 1;
                } /<ID>/ && i {
                        ID = ID RS $3;
                        c++;
                } /<\/DeviceIDList/ && f {
                        i = 0;
                } /<Name>/ && f {
                        NM = $3;
                } /<\/Group>/ && f {
                        f = 0;
                } END {
                        printf "Group Info: %s (%d)\n", NM, V;
                        print "Device in group: "
                        print ID;
                        printf "Total in group: %d\n", c;
} ' xml_file

These 2 Users Gave Thanks to Yoda For This Post:
# 5  
Old 02-13-2013
Thank you!!

Thank you so much for taking the time to respond. I will try out the awk code and see how it works.

Thanks again!!!!

---------- Post updated at 11:32 AM ---------- Previous update was at 11:27 AM ----------

One note of clarification please: Why do you have the "-v" option set? What does it do?

I think that you might have thought that I only have one group (Group Info: Las Vegas SERVERS (39786)) The xml file has 100's of Groups. That's why I am looking to have the script list All groups with their associated Devices. Does that make sense or am I confusing the issue?
# 6  
Old 02-13-2013
Quote:
Originally Posted by dlundwall
One note of clarification please: Why do you have the "-v" option set? What does it do?

I think that you might have thought that I only have one group (Group Info: Las Vegas SERVERS (39786)) The xml file has 100's of Groups. That's why I am looking to have the script list All groups with their associated Devices. Does that make sense or am I confusing the issue?
Yes, that is what I thought and code looks only for 39786.

For grouping all others you can code something like:
Code:
awk -F'[<>]' '  /<Group version/ {
                        getline;
                        GID = $3;
                        f = 1;
                        next;
                } /<DeviceIDList/ && f {
                        i = 1;
                } /<ID>/ && i {
                        ID = ID RS $3;
                        c++;
                } /<\/DeviceIDList/ && f {
                        i = 0;
                } /<Name>/ && f {
                        NM = $3;
                } /<\/Group>/ && f {
                        f = 0;
                        printf "Group Info: %s (%d)\n", NM, GID;
                        print "Device in group: "
                        print ID;
                        printf "Total in group: %d\n", c;
                        NM = ""; GID = ""; ID = ""; c = 0;
} ' xml_file

I hope this helps.
This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Sql ORA-00937: not a single-group group function

I'm trying to return only one row with the highest value for PCT_MAX_USED. Any suggestions? When I add this code, I get the ORA-00937 error. trunc(max(decode( kbytes_max, 0, 0, (kbytes_alloc/kbytes_max)*100))) pct_max_used This is the original and returns all rows. select (select... (3 Replies)
Discussion started by: progkcp
3 Replies

2. Shell Programming and Scripting

Shell script - group by

Hi, I have text file as shown below. root 25 oracle 25 batch 30 griduser 32 admin 35 root 25 oracle 25 batch 30 griduser 32 oracle 25 batch 30 griduser 32 xuser 45 admin 35 I want to group by based on user name, and the output need to be as below. Not necessary the username to be... (10 Replies)
Discussion started by: baladelaware73
10 Replies

3. Shell Programming and Scripting

need a one liner to grep a group info from /etc/group and use that result to search passwd file

/etc/group tiadm::345:mk789,po312,jo343,ju454,ko453,yx879,iy345,hn453 bin::2:root,daemon sys::3:root,bin,adm adm::4:root,daemon uucp::5:root /etc/passwd mk789:x:234:1::/export/home/dummy:/bin/sh po312:x:234:1::/export/home/dummy:/bin/sh ju454:x:234:1::/export/home/dummy:/bin/sh... (6 Replies)
Discussion started by: chidori
6 Replies

4. AIX

Adding a Volume Group to an HACMP Resource Group?

Hi, I have a 2 node Cluster. Which is working in active/passive mode (i.e Node#1 is running and when it goes down the Node#2 takes over) Now there's this requirement that we need a mount point say /test that should be available in active node #1 and when node #1 goes down and node#2 takes... (6 Replies)
Discussion started by: aixromeo
6 Replies

5. Shell Programming and Scripting

Sort the file contents in each group....print the group title as well

I've this file and need to sort the data in each group File would look like this ... cat file1.txt Reason : ABC 12345-0023 32123-5400 32442-5333 Reason : DEF 42523-3453 23345-3311 Reason : HIJ 454553-0001 I would like to sort each group on the last 4 fileds and print them... (11 Replies)
Discussion started by: prash184u
11 Replies

6. Shell Programming and Scripting

Merge group numbers and add a column containing group names

Hi All I do have a file like this with 6 columns. Groups of data merge together and the group number is indicated above each group. 1 1 12 26 289 3.2e-027 GCGTATGGCGGC 2 12 26 215 6.7e+006 TTCCACCTTTTG 3 9 26 175 ... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

7. Shell Programming and Scripting

Merge group numbers and add a column containing group names

I have a file in the following format. Groups of data merge together and the group number is indicated above each group. 1 adrf dfgr dfg 2 dfgr dfgr 3 dfef dfr fd 4 fgrt fgr fgg 5 fgrt fgr (3 Replies)
Discussion started by: Lucky Ali
3 Replies

8. Shell Programming and Scripting

How to remove xml namespace from xml file using shell script?

I have an xml file: <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3> <Table1> </AutoData> and I have to remove the portion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" only. I tried using sed... (10 Replies)
Discussion started by: Gary1978
10 Replies

9. Solaris

entry in /etc/group too long - problem using sudo with %group

hi folks, I've been googling for quite some time, but still can't find anything near it...my problem is the following: for useradministration in our company we are using ssh/sudo, now whenever I try to add users (we have quite a number of users) with useradd -G groupname for secondary group I... (4 Replies)
Discussion started by: poli
4 Replies
Login or Register to Ask a Question