Hi,
Need help with parsing xml data in unix and place it in a csv file. My xml file looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<iwgroups>
<nextid value="128">
</nextid>
<iwgroup name="RXapproval" id="124" display-name="RXapproval" description="group for RX approval ">
<user name="iwov">
</user>
<user name="m161595">
</user>
<user name="m594670">
</user>
<work name="iwov">
</work>
</iwgroup>
<iwgroup name="TEXT_EMAIL" id="113" display-name="" description="TEXT email group with permission to start text pool workflow">
<user name="iwov">
</user>
<user name="m123053">
</user>
<user name="m270857">
</user>
<user name="m363836">
</user>
I would need to place the output in a csv file in below format:
Groupname, Description, Users
RXapproval, group for RX approval, iwov m161595 m594670
TEXT_EMAIL, TEXT email group with permission to start text pool workflow, iwov m123053 m270857 m363836
Please let me know on what is the best way to do this.
You have asked for our help fourteen times. What have you tried to solve this problem?
An XML file usually has matching open and close tags. The sample input you have provided has one opening iwgroups tag and no matching closing tag. The sample input you have provided has two opening iwgroup tags, but only one closing tag. It would seem that the closing iwgroup tag would be the trigger that should terminate processing of an XML input segment and produce an output line in your desired CSV file. How can we be expected to suggest code if you can't give us consistent sample input and output?
A CSV file (with comma as the character separating values) doesn't usually have LOTS of leading and/or trailing spaces in the data. Why are there dozens of seemingly extraneous spaces in your desired sample output?
A quoted field in an XML file usually specifies that the data between the quotes should be the contents of the output field you want in your CSV file. The first description field in your XML file is:
description="group for RX approval "
(note the trailing <space> after approval ), but the output you say you want for that field is:
group for RX approval
(note the eight leading <space> characters that are not present in the XML file and the missing trailing space after approval ).
If you want our help on this you need to very clearly describe the output format you are trying to produce; not just show us a desired CSV file that does not seem to match the sample XML file you have provided.
Hi Don,
Sorry for the confusion. Please find my input below:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<iwgroups>
<nextid value="128">
</nextid>
<iwgroup name="AEapproval" id="124" display-name="AEapproval" description="group for AE approval">
<user name="iwov">
</user>
<user name="m161595">
</user>
<user name="m594670">
</user>
<user name="m803051">
</user>
</iwgroup>
<iwgroup name="OES_EMAIL" id="113" display-name="" description="OES email group with permission to start oes pool workflow">
<user name="iwov">
</user>
<user name="m123053">
</user>
<user name="m270857">
</user>
<user name="m363836">
</user>
</iwgroup>
</iwgroups>
I just need a report with the following fields:
Groupname1:AEapproval
Description: group for AE approval
Users: iwov, m161595, m594670,m803051
Groupname2:OES_EMAIL
Description: OES email group with permission to start oes pool workflow
Users: iwov,m123053,m270857,m363836
Output may not be in a csv file, a text file will also do in the above format. Hope I am clear this time.
Groupname,Description,Users
AEapproval,group for AE approval,iwov m161595 m594670 m803051
OES_EMAIL,OES email group with permission to start yes pool workflow,iwov m123053 m270857 m363836
with your latest sample input.
If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .
Save as report.pl
Run as perl report.pl ajayakunuri.file > ajayakunuri.report
perl report.pl ajayakunuri.file
Groupname1: AEapproval
Description: group for AE approval
Users: iwov, m161595, m594670, m803051
Groupname2: OES_EMAIL
Description: OES email group with permission to start oes pool workflow
Users: iwov, m123053, m270857, m363836
$group[0]->[0] is a dereference to the first element of a referenced array.
The Users: part has nothing to do with the variable, it is that close to it because of the concatenation and it will display as the beginning of another line since $group[0]->[0] will return a string with a newline already.
Perhaps a display of the whole data structure through the loop, might help you:
$VAR1 = [
[
'Groupname1: AEapproval
Description: group for AE approval
'
],
[
'iwov',
'm161595',
'm594670',
'm803051'
]
];
$VAR1 = [
[
'Groupname2: OES_EMAIL
Description: OES email group with permission to start oes pool workflow
'
],
[
'iwov',
'm123053',
'm270857',
'm363836'
]
];
If you're struggling to modify this code to your need in another situation, please, open another thread and post what you have done and we will take a look at it.