![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
replace string in XML with sed
Greetings,
I have an XML : file.xml Code:
<component>
<name>abcd</name>
<value>1234</value>
</component>
Code:
sed 's/<name>[-[:alnum:]./]\{1,\}<\/name>/<name>ijkl<\/name>/' file.xml > newfile.xml
Quote:
Can somebody please advise. Thanks, Chiru Last edited by Yogesh Sawant; 04-08-2008 at 01:45 PM.. Reason: added code tags |
|
||||
|
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 |
|
||||
|
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.
|
|
|||||
|
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>
Code:
$ xsltproc -param NAME "'Finnbarr'" file.xsl file.xml
<?xml version="1.0"?>
<component>
<name>Finnbarr</name>
<value>1234</value>
</component>
$
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| linux, solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|