The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search, replace string in file1 with string from (lookup table) file2? gstuart Shell Programming and Scripting 9 06-08-2009 06:11 AM
replace string sam99 Shell Programming and Scripting 4 03-04-2008 01:39 AM
SED Replace String Help prash184u Shell Programming and Scripting 2 01-23-2008 01:57 AM
replace a string melanie_pfefer Shell Programming and Scripting 11 01-17-2008 10:57 AM
Replace string B depending on occurence of string A hemangjani Shell Programming and Scripting 1 12-05-2006 05:10 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-08-2008
chiru_h chiru_h is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 72
replace string in XML with sed

Greetings,
I have an XML : file.xml
Code:
<component>
       <name>abcd</name>
       <value>1234</value>
</component>
I am using sed to replace abcd with the desired value dynamically without knowing the actual value.
Code:
sed 's/<name>[-[:alnum:]./]\{1,\}<\/name>/<name>ijkl<\/name>/' file.xml > newfile.xml
I don't have any issues with this command in Linux but on Solaris machine, I am getting the error:
Quote:
sed: command garbled: /<name>[-[:alnum:]./]\{1,\}<\/name>/<name>ijkl<\/name>/
If I remove >[-[:alnum:]./]\{1,\} and put the actual value, it is fine. But I have do it dynamically as I use it within the script to replace the existing value with the given value.

Can somebody please advise.

Thanks,
Chiru

Last edited by Yogesh Sawant; 04-08-2008 at 01:45 PM.. Reason: added code tags
  #2 (permalink)  
Old 04-08-2008
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,206
I am surprised it works anywhere, the command is garbled in at least one way. There are 4 / characters in your expression.

I'm not at a solaris box right now, but I'm pretty sure Solaris sed does not support [::] format expressions, so you would get more change from something like:

Code:
sed 's#<name>\([^<][^<]*\)</name>#<name>SOMETHING</name>#' file.xml
  #3 (permalink)  
Old 04-08-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
If you replace the whole line this should be suffice:

Code:
sed 's#<name>.*#<name>SOMETHING</name>#' file.xml
Regards
  #4 (permalink)  
Old 04-08-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
you should use a tool specifically for parsing XML, not sed. (although it still can be done)
  #5 (permalink)  
Old 04-09-2008
chiru_h chiru_h is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 72
Thanks Reborg/Franklin..both of them work, but am using Reborg's as the other one is changing in some other places where I don't need.

What will be the easiest way of XML parsing - in the sense that I can use as part of the shell script I have which does several other tasks as well.

Thanks,
Chiru
  #6 (permalink)  
Old 04-09-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The least painful way depends on your other requirements. If the file is simple then simple line-oriented shell utilities you are familiar with should usually suffice. If you need to do anything which requires any real understanding of XML structures (nesting, sibling and similar relationships, conditionals etc) then it probably makes sense to get at least some introductory familiarity to some proper XML tool. There are several formalisms to choose from and many tools which implement them; personally, I've been able to get things done with xsltproc after the first shock of trying to understand what got into the heads of the people who came up with the specifications for this.
  #7 (permalink)  
Old 04-09-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,912
The best way of handling this type of problem is using XSLT and a stylesheet.

Here is a stylesheet (file.xsl) which will do what you want:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="NAME"/>

<xsl:template match="name">
    <name><xsl:value-of select="$NAME"/></name>
</xsl:template>

<xsl:template match="*|text()|@*">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
Assuming for the purpose of this example that you want to change "abcd" to "Finnbarr" and that you have xsltproc installed on your system, here is how you would do the transformation together with the resulting output:

Code:
$ xsltproc -param NAME "'Finnbarr'" file.xsl file.xml
<?xml version="1.0"?>
<component>
       <name>Finnbarr</name>
       <value>1234</value>
</component>
$
Note the double and single quotes around the vale of the NAME parameter. These are required.
Sponsored Links
Closed Thread

Bookmarks

Tags
linux, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:35 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0