LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   parse xml and extract the attribute value by search tag in shell (https://www.linuxquestions.org/questions/linux-newbie-8/parse-xml-and-extract-the-attribute-value-by-search-tag-in-shell-4175578552/)

boby.kumar 04-28-2016 08:29 AM

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

syg00 04-28-2016 08:36 AM

The similar threads below show you have similar queries that have been answered. If you don't take that advice, why ask again ?.

TB0ne 04-28-2016 08:44 AM

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.

boby.kumar 04-29-2016 12:37 AM

thanks TBOne for update. but seems that you have some issue regarding my queries.

TB0ne 04-29-2016 02:08 PM

Quote:

Originally Posted by boby.kumar (Post 5538136)
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.


All times are GMT -5. The time now is 11:05 AM.