Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

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)
4 REPLIES
H.Becker
Honored Contributor
Solution

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

$
James R. Ferguson
Acclaimed Contributor

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...
Highlighted
Bill McNAMARA_1
Honored Contributor

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)
James R. Ferguson
Acclaimed Contributor

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...