Re: script to search an xml file and output all sections containing input string..... (58 Views)
Reply
Honored Contributor
Bill McNAMARA_1
Posts: 3,866
Registered: ‎11-14-2000
Message 1 of 5 (58 Views)
Accepted Solution

script to search an xml file and output all sections containing input string.....

Hi all,

I'm looking for a way to query an xml file for a particular property with content such as follows:





5



9



6



91



The result of the query for

script.sh 12345

for example would be



5



91


How could this be done with sed/awk and other standard tools!

Thanks for any help suggestions.
Regards,
Bill
It works for me (tm)
Please use plain text.
Esteemed Contributor
H.Becker
Posts: 363
Registered: ‎04-09-2009
Message 2 of 5 (58 Views)

Re: script to search an xml file and output all sections containing input string.....

This one works for me:
$ cat x.sh
#! /bin/sh
awk "BEGIN {RS=\"\"} /.*$1.*/ {printf \$0\"\"} END {printf \"\n\"}" $2
$
$ ./x.sh 12345 x.x


5



91

$
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 5 (58 Views)

Re: script to search an xml file and output all sections containing input string.....

Hi Bill:

# cat ./myfilter
#!/usr/bin/perl
use strict;
use warnings;
my $pattern = shift or die;
undef $/;
my $xml = ;
for ( split( //, $xml ) ) {
print "", $_ if m/=$pattern>/;
}
1;

...run as:

# ./myfilter 12345 file


5



91



Regards!

...JRF...
Please use plain text.
Honored Contributor
Bill McNAMARA_1
Posts: 3,866
Registered: ‎11-14-2000
Message 4 of 5 (58 Views)

Re: script to search an xml file and output all sections containing input string.....

Hi,
Great thanks for your rapid answers.
James, I got an error with your script with the input.xml shown above...

My perl isn't good enough to correct it yet!

[root@oces01 scripts]# ./searchCDR.2.sh 12345 cdr.xml
Name "main::DATA" used only once: possible typo at ./searchCDR.2.sh line 6.
readline() on unopened filehandle DATA at ./searchCDR.2.sh line 6.
Use of uninitialized value in split at ./searchCDR.2.sh line 7.

It works for me (tm)
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 5 (58 Views)

Re: script to search an xml file and output all sections containing input string.....

Hi (again) Bill:

> James, I got an error with your script with the input.xml shown above...

Yes, my apologies. Change the line that reads:

my $xml = ;

...to be:

my $xml = <>;

The use of allowed me to include your actual data as a part of the script by adding it at the script's end with the prepended line '__DATA__'. When I dropped this I meant to make the change above to enable one or more files to be read from the command line.

Regards!

...JRF...
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation