Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extract string from XML file 1

Status
Not open for further replies.

Draoued

MIS
Aug 1, 2002
378
FI
Hi

I m ashamed to post such question but I can not figure out how to do it.

I have a big XML file , with lots of tag.
One of the line contains this:
Code:
<reportViewSearchPath>CAMID("MYportal Security:u:authid=4184878700")/folder[@name='Blablabl bala']/reportView[@name='My report Test']</reportViewSearchPath>

What I want to do is to extract "My report Test" and replace the space by underscore , so the expected result is My_report_Test


the beginning of my script looks like this
Code:
grep 'reportView\[@name=' /usr/local/../xml/myfile.xml >temp.txt
cut -d']</reportViewSearchPath>' -f2 temp.txt >temp2.txt
Result=$(cut -d'@name=' -f2 temp2.txt)
echo $Result

It's really huggly, that's why I ask you for some hits.
I have the impression that it could be possible to do everything in one line, and not going through intermediate temp files.
I think there is also problems with the [ char in the xml file and it seems that my cut command does like.
 
Hi

If I understood you correcty :
Code:
sed "/@name=/{s/.*reportView\[@name='\([^']*\)'\].*//;y/ /_/;p}" myfile.xml
( Note : tested with GNU [tt]sed[/tt]. )

By the way, [tt]cut[/tt]'s delimiter must be a single character, so you can not use it that way.

Feherke.
 
thanks
I get an error Sed command cannot be parsed
and there are 2 @name strings in the line , would that be a problem ??
 
something like this ?
Result=$(sed -n "s! !_!g;s!.*/reportView\[@name='\([^']*\)'.*!\1!p" /usr/local/../xml/myfile.xml)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV it works perfectly.
Now I have to read your command and trying to understand it.

Sorry Feherke, I don't know which sed I m running.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top