The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Capturing stdin domivv Shell Programming and Scripting 3 01-24-2008 01:30 PM
capturing log mgirinath Shell Programming and Scripting 3 05-16-2006 08:21 AM
Capturing text between () sysera Shell Programming and Scripting 7 06-14-2005 08:37 AM
Capturing value into variable prasad01 Shell Programming and Scripting 2 08-19-2003 12:38 PM
capturing process id bvreddy Shell Programming and Scripting 1 09-16-2002 01:08 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 05-14-2008
Registered User
 

Join Date: May 2008
Posts: 1
Post Capturing string between a xml tag

Hi All,
I have an XML-:
Code:
       <ProcId>CES_P5010_AddVLan</ProcId>
        <DataVersion>yxcxycyxcycyxc</DataVersion>
        <JobId>OR3000055-002-1</JobId>
          </CesHeader>
    <VLanServiceList>
        <NopId>blu</NopId>
    </VLanServiceList>
            <StatusNPA>2</StatusNPA>
        <ErrorTextNPA>Exception occurred trying to create Management Service :Exception occurred trying to create Service :Error Code=239023  [Virtual LAN: ].. ** </ErrorTextNPA>
    </NPAErrorHandling>
</CesResponse>


Here I want to have a shell script to get the string in the tag <ErrorTextNPA>*****<ErrorTextNPA>. Can any one help me on this.
Regards
Amit

Last edited by Yogesh Sawant; 05-14-2008 at 04:17 AM. Reason: added code tags
Reply With Quote
Forum Sponsor
  #2  
Old 05-14-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Is this a single long line, or spanning over multiple lines?

As such, the following doesn't really care.

Code:
perl -0777 -pe 's%.*<ErrorTextNPA>%%s;s%</ErrorTextNPA>.*%%s' file
Reply With Quote
  #3  
Old 05-14-2008
DukeNuke2's Avatar
Soulman
 

Join Date: Jul 2006
Location: Germany, Berlin
Posts: 1,460
moved the thread from solaris to shell scripting for this is not a solris specific question...
Reply With Quote
  #4  
Old 05-14-2008
Registered User
 

Join Date: Apr 2008
Location: Bangalore
Posts: 123
using sed,

cat filename | grep -e '<ErrorTextNPA>' | sed 's/<ErrorTextNPA>\(.*\)<\/ErrorTextNPA>/\1/'
Thanks
Penchal
Reply With Quote
  #5  
Old 05-14-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Actually the cat and grep are redundant.

Code:
sed -n 's%.*<ErrorTextNPA>\(.*\)</ErrorTextNPA>.*%\1%p' file.xml
However, it won't work if the open and close tags are on different lines. (There are ways to cope with that in sed, too, of course.)
Reply With Quote
  #6  
Old 05-14-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/florida
Posts: 1,045
Quote:
<ProcId>CES_P5010_AddVLan</ProcId>
<DataVersion>yxcxycyxcycyxc</DataVersion>
<JobId>OR3000055-002-1</JobId>
</CesHeader>
<VLanServiceList>
<NopId>blu</NopId>
</VLanServiceList>
<StatusNPA>2</StatusNPA>
<ErrorTextNPA>Exception occurred trying to create Management Service :Exception occurred trying to create Service :Error Code=239023 [Virtual LAN: ].. **
</ErrorTextNPA>
</NPAErrorHandling>
</CesResponse>
The best way to handle this type of problem is to use an XSLT processor together with an XSL stylesheet to transform the XML document into whatever output you want - in this case the contents of the XML element <ErrorTextNPA>. UNIX command line tool such as awk, sed, etc were never designed to efficiently handle XML documents.

I would note that this is not possible for the supplied XML document since it is not a well-formed or valid XML document (missing root, missing opening tags, etc.)
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
solaris

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0