Operating System - Linux
1828343 Members
3057 Online
109976 Solutions
New Discussion

Using sed to obtain values of XML tag from several XML files

 
Danny Fang
Frequent Advisor

Using sed to obtain values of XML tag from several XML files

HI,

I'm attempting to extract the values of the XML tag from the XML file attached in this posting. The values to be extracted are from the tags , and as shown below:

pmNoOfNonHoReqDeniedCs
pmNoOfNonHoReqDeniedInteractive
pmNoOfNonHoReqDeniedSpeech

I've attempted using "sed" to perform this task on a Linux RH9 machine.

$sed -n 's|\(.*\)|\1|p' sample.xml
pmNoOfNonHoReqDeniedCs

$sed -n 's|\(.*\)|\1|p' sample.xml
pmNoOfNonHoReqDeniedInteractive

$sed -n 's|\(.*\)|\1|p' sample.xml
pmNoOfNonHoReqDeniedSpeech

The "sed" options supplied is able to return the values between the XML tags.

However, I'm unsure how I could loop through the contents of several *.xml files within the same directory and assign the values of , and tags to some variable for display.

Could any help me out?

Thanks
3 REPLIES 3
Alexander Chuzhoy
Honored Contributor

Re: Using sed to obtain values of XML tag from several XML files

You can use the following method:
sed -n 's|<[mtes]*>\(.*\)<[mtes/]*>|\1|p' sample.xml
Stuart Browne
Honored Contributor

Re: Using sed to obtain values of XML tag from several XML files

I'd use:

sed -n 's|<[mes]t>\(.*\)|\1|p' sample.xml

forces the tag matching to be a little more accurate.

But to be truely accurate, you should use backreferencing regular expressions:

sed -n 's|<\([mes]t\)>\(.*\)|\2|p' sample.xml

As for reading these values in, a simple while loop with a read should do, i.e.:

while read LINE
do
echo My value is: $LINE
done < <(sed -n 's|<\([mes]t\)>\(.*\)|\2|p' sample.xml)

(NOTE: This is for a 'bash' shell. For older shells, you'd do 'sed .... | while read LINE' instead)
One long-haired git at your service...
Antonio Cardoso_1
Trusted Contributor

Re: Using sed to obtain values of XML tag from several XML files

Danny,

From various posts you sent in HP-UX and this forums, many of task you are attempting to address using shell would have more simple, reliable and maintenable solutions using PERL scripting instead of shell.
Maybe consider learning this as a long term investment... as becoming efficient with perl is not achieved in one day...

For my own needs, and depending on task and data complexity, I use either SHELL/awk/sed mix or full perl scripts.

Now for the question in this thread, the PERL answer would be to delegate interpretation of your xml input file to XML::Parser and XML::SimpleObject modules.

regards,
antonio.