Operating System - HP-UX
1832961 Members
3160 Online
110048 Solutions
New Discussion

Re: xml search/parse script

 
SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

xml search/parse script

Hi, I'm trying to create a script to analyse an xml file in the following format.


BeerStore
1 street



Guinness
Stout
6


Budweiser
Larger
6




SpiritStore
2 street



Baileys
Cream from the center of the Earth
2


Paddy
Whiskey
2




Now I'd like to do a search on
Say 'Paddy' and find out 'HowMany' are due and in which 'Shop' to order through script.

Something like this:
#./script Paddy
You get Paddy in SpiritStore, 2 street and you need 2
Have a great day!!
#

Any ideas on how to proceed!?
All suggestions welcome/rewarded!

Later,
Bill
It works for me (tm)
10 REPLIES 10
Robin Wakefield
Honored Contributor
Solution

Re: xml search/parse script

Hi Bill

create awk script as follows:
================================
$0 ~ search{GOTIT=1}
//{NAME=substr($0,7,length($0)-14)}
/
/{GOTNAME=NAME;ADDRESS=substr($0,10,length($0)-20)}
//{HOWMANY=substr($0,10,length($0)-20)
if (GOTIT == 1) {print "You get",NAME,"in",ADDRESS,"and you need",HOWMANY};GOTIT=0}

==============================

and run it with:

awk -f above-script search=Paddy inputfile

Rgds, Robin
Robin Wakefield
Honored Contributor

Re: xml search/parse script

Hi Bill,

Sorry, the last line should read:

if (GOTIT == 1) {print "You get",NAME,"in",GOTNAME",",ADDRESS,"and you need",HOWMANY};GOTIT=0}

Rgds, Robin
Bill McNAMARA_1
Honored Contributor

Re: xml search/parse script

I have absolutely no idea how it works, but it does!!

Thanks Robin.
Bill
It works for me (tm)
Robin Wakefield
Honored Contributor

Re: xml search/parse script

Hi Bill,

And no, that wasn't a deliberately-forget-something-to-get-more-points reply, but thanks anyway!!

Rgds, Robin.
Bill McNAMARA_1
Honored Contributor

Re: xml search/parse script

Apologies Robin, my knowledge of awk is limited to print..
I need your help a bit more!

I changed the text to match my xml files and am getting some problems..
This is the real text:

The o/p should be
A search for lets say 543

returning within
data_00_33 whatever..


---------------------
-
data_00_33
E1
ISUP
10
rio.0.0.33
+
31
CIC-NO
10
543

+
30
CIC-NO
10
542

+
29
CIC-NO
10
541


28
CIC-NO
10
540


27
CIC-NO
10
539


26
CIC-NO
10
538


25
CIC-NO
10
537

It works for me (tm)
Bill McNAMARA_1
Honored Contributor

Re: xml search/parse script

# awk -f /tmp/script.awk search=7 /tmp/Conf_Pereal.xm>
You get in , and you need p
You get in , and you need p

awk script:
$0 ~ search{GOTIT=1}
//{VALUE=substr($0,7,length($0)-14)}
//{GOTNAME=VALUE;TRUNKGROUP=substr($0,10,length($0)-20)}
//{TRANSPORTMAPPING=substr($0,10,length($0)-20)
if (GOTIT == 1) {print "You get",VALUE,"in",GOTNAME",",TRUNKGROUP,"and you need"
,TRANSPORTMAPPING};GOTIT=0}

xml attached
It works for me (tm)
Robin Wakefield
Honored Contributor

Re: xml search/parse script

Hi Bill,

Try this:

=============================
$0 ~ "" search{GOTIT=1}
{sub(/^ */,"",$0)}
//{TRUNKGROUP=substr($0,7)}
//{TRANSPORTMAPPING=substr($0,19) }
//{VALUE=substr($0,8)
if (GOTIT == 1) {print "You get",VALUE,"in Trunkgroup",
TRUNKGROUP,"and you need Mapping",TRANSPORTMAPPING};GOTIT=0}

=============================

The file you attached was slightly different to the initial format in that, for instance
was on a line of its own. I have assumed only ever refers to a Trunkgroup.

If you want to play with the script, the figure in the substr function is the length of the search string + 1, e.g. is 6 chars + 1 =7.

The sub at the beginning removes leading spaces. I also assume you only want to search on the parameter.

Anyway, give it a go, let me know of any problems.

Rgds, Robin.
Bill McNAMARA_1
Honored Contributor

Re: xml search/parse script

Thanks!.. I'm going to have to learn awk..

In anycase, The o/p is something like the following now, which is perfectly correct... but just for cosmetic reasons, how could I get rid of the and others.. without limiting the fields to character lengths..

CIC: 12345678 found
Trunkgroup: TG_00_00
Transport Mapping: pereal.0.0.0
Thanks again,
Bill
It works for me (tm)
Robin Wakefield
Honored Contributor

Re: xml search/parse script

Hi Bill,

OK, sounds like I was working on a slightly different file format, but it's easy enough to remove the trailing strings.

If you have a look back to my original script, I have a third argument to the substr function - this tells awk to only output a string of this length. This will equal the length of the original line, length($0), minus the length of, say, + . You may need to adjust this figure if you have trailing spaces, as I did.

Got to dash now, but hopefully that will help.

Rgds, Robin
Bill McNAMARA_1
Honored Contributor

Re: xml search/parse script

have a good weekend Robin,
Thanks for the help.

Bill
It works for me (tm)