LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-21-2010, 12:58 AM   #1
testac
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Rep: Reputation: 0
extract attribute value from xml using bash


Hi ,

I have a xml like

Code:
<?xml version="1.0"?><Response>
<CompSearchResult>
<Composite comp5Y="640.8817" comp7Y="657.64222" comp10Y="677.662674" comp15Y="654.737201" comp20Y="680.628806" comp30Y="654.571273"></Composite>
<msg>OK</msg>
</CompSearchResult>
</Response>
Now iam using below code to read each of values of attribute like comp5Y,comp7Y,comp10Y,comp15Y,comp20Y,comp30Y

Code:
VAL_5Y=`cat wh.xml|grep "comp5Y="| cut -d\" -f2`
VAL_7Y=`cat wh.xml|grep "comp7Y="| cut -d\" -f2`
VAL_10Y=`cat wh.xml|grep "comp10Y="| cut -d\" -f2`
The problem Iam facing is that the value in VAL_5Y,VAL_7Y,VAL_10Y is all same as of VAL_5Y as the cut is only looking for first occurrence of double quotes . So how can i modify the code to get each of the values of 5Y,7Y,10Y etc ..

Thanks in advance.
 
Old 06-21-2010, 01:04 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
Assuming you wish to keep the same commands, simply change the value to the '-f' option for cut.
So based on your input the following should work:
Code:
VAL_7Y=`cat wh.xml|grep "comp7Y="| cut -d\" -f4`
Note: This requires you to now the position of each value.
 
Old 06-21-2010, 01:32 AM   #3
testac
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Original Poster
Rep: Reputation: 0
thanks grail for reply but actually iam not looking for that solution .
Actually in output not necessary everytime 5Y,7Y,10Y comes as if value is only there then only the field appear . So basically Iam looking for much more generic pattern search as Iam only sure of 1 thing i.e if value is there then it will surely start with comp so comp*Y="<number with(out) decimal>" is the format i want to parse .
 
Old 06-21-2010, 02:08 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
Well there will be others that will have a Perl option as it can parse xml files, but lacking that and assuming "Composite" only appears once, this should work:
Code:
eval $(awk '/Composite/{gsub(/"|>.*/,"");for(x=2;x<=NF;x++)print "VAL_"substr($x,5)}' xml_file)
 
Old 06-21-2010, 03:03 AM   #5
testac
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Original Poster
Rep: Reputation: 0
thanks grail ..it works.
 
Old 06-21-2010, 03:18 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,719

Rep: Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034Reputation: 3034
Cool ... don't forget to mark as SOLVED if you have your final solution.
 
Old 06-21-2010, 05:35 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Slack, Debian, Mint, Puppy, Raspbian
Posts: 3,465

Rep: Reputation: 220Reputation: 220Reputation: 220
using bash for parsing XML!??!

I hope you aren't designing a nuclear reactor cooling system

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: Extract First Seven Characters To Variable Grizzlyman Programming 4 10-19-2009 12:41 AM
update problem "Attribute error object has no attribute 'rsplit' ruse Linux - Newbie 0 08-28-2009 01:46 AM
ldap_add: Undefined attribute type (17) additional info: ojectclass: attribute type vinaytp Linux - Newbie 2 05-28-2009 04:57 AM
setting XML attribute using parameter in XSLT mohtasham1983 Programming 2 01-03-2008 04:03 PM
How to Extract a .xml.bz2 file? en3r0 Linux - Software 2 02-21-2006 09:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration