LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-28-2016, 08:29 AM   #1
boby.kumar
Member
 
Registered: Mar 2013
Posts: 94

Rep: Reputation: Disabled
parse xml and extract the attribute value by search tag in shell


Hi Team,
I am working on a automation part where required to open some xml files and extract the attribute values and store in a variable. all xml files are different like-

Quote:
localhost:user1: cat response1.xml
<?xml version="1.0" encoding="UTF-8"?>
<IO><STATUS DESCRIPTION="FAILURE" CODE="04"/></IO>

localhost:user1: cat response2.xml
<?xml version="1.0" encoding="UTF-8"?>
<IO xmlns="http://uk.co.vodafone.vm.crp.barringapi.getbars.response"><STATUS DESCRIPTION="SUCCESS" CODE="00"/><BARS><ns1:BAR xmlns:ns1="http://uk.co.vodafone.vm.crp.barringapi.common" barred="18 Rated Content &amp; Services" classification="Content Control" type="Content Category" authority="50" proveAge="true" isDefault="Y"/></BARS></IO>

localhost:user1: cat response3.xml
<?xml version="1.0" encoding="UTF-8"?><IO><STATUS DESCRIPTION="FAILURE" CODE="04"/></IO>

localhost:user1: cat response4.xml
<STATUS DESCRIPTION="FAILURE" CODE="04"/></IO>
I am using two shell command to extract the exact CODE value but failing of these xml's.

Quote:
localhost:user1: grep "<IO>" response1.xml | awk -F""" '{print $4}'
04

localhost:user1: grep "CODE" response1.xml | awk -F""" '{print $4}'
04

localhost:user1: grep "CODE" response2.xml | awk -F""" '{print $4}'
SUCCESS

localhost:user1: grep "CODE" response3.xml | awk -F""" '{print $4}'
UTF-8

localhost:user1: sed "s/.* CODE="\(.*\)".*/\1/" response2.xml
<?xml version="1.0" encoding="UTF-8"?>
00"/><BARS><ns1:BAR xmlns:ns1="http://uk.co.vodafone.vm.crp.barringapi.common" barred="18 Rated Content &amp; Services" classification="Content Control" type="Content Category" authority="50" proveAge="true" isDefault="Y

localhost:user1: sed "s/.* CODE="\(.*\)".*/\1/" response3.xml
04

localhost:user1: sed "s/.* CODE="\(.*\)".*/\1/" response4.xml
04

localhost:user1: grep "CODE" response4.xml | awk -F""" '{print $4}'
04
problem is that both command gives the different output if the xml file change and automation fails there.

can anyone help here to extract the only CODE value without checking the files content in a single command.

thanks in Advance
 
Old 04-28-2016, 08:36 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,127

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
The similar threads below show you have similar queries that have been answered. If you don't take that advice, why ask again ?.
 
1 members found this post helpful.
Old 04-28-2016, 08:44 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Agree totally with syg00....you've been here three years now, OP, and have asked us REPEATEDLY to write scripts for you, and don't seem to take advice when it's given.

There are MANY XML parsing tools available to you...Perl has several that are excellent, as does Python. There are even bash tools to do this, like xmllint, which you could find with a brief Google search. Parsing XML with sed isn't a good idea.
 
1 members found this post helpful.
Old 04-29-2016, 12:37 AM   #4
boby.kumar
Member
 
Registered: Mar 2013
Posts: 94

Original Poster
Rep: Reputation: Disabled
thanks TBOne for update. but seems that you have some issue regarding my queries.
 
Old 04-29-2016, 02:08 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by boby.kumar View Post
thanks TBOne for update. but seems that you have some issue regarding my queries.
Because you don't show effort of your own, despite being asked to many times. There are thousands of links that are easily found with a brief Google search.
 
  


Reply



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
Extract values between xml tag boby.kumar Linux - Newbie 2 11-02-2015 10:31 AM
[SOLVED] Trying to parse a value from XML tag for a variable n3wtux Linux - Newbie 1 07-26-2013 04:51 PM
How to search for a corresponding tag in xml file and replace the corresponding value userofunix Linux - Newbie 1 09-17-2012 02:42 PM
[SOLVED] Parse XML tags with attribute with SED adamzuber Programming 4 04-09-2012 10:37 PM
extract attribute value from xml using bash testac Programming 6 06-21-2010 05:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:56 AM.

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
Open Source Consulting | Domain Registration