replace string in XML with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace string in XML with sed
# 1  
Old 04-08-2008
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 02:45 PM.. Reason: added code tags
# 2  
Old 04-08-2008
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  
Old 04-08-2008
If you replace the whole line this should be suffice:

Code:
sed 's#<name>.*#<name>SOMETHING</name>#' file.xml

Regards
# 4  
Old 04-08-2008
you should use a tool specifically for parsing XML, not sed. (although it still can be done)
# 5  
Old 04-09-2008
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  
Old 04-09-2008
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  
Old 04-09-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed search and replace after xml tag

Hi All, I'm new to sed. In following XML file <interface type='direct'> <mac address='52:54:00:86:ce:f6'/> <source dev='eno1' mode='bridge'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> ... (8 Replies)
Discussion started by: varunrapelly
8 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. Shell Programming and Scripting

Replace a string in a xml file

Hello, I have below xml file, I want to find line default-value and replace the string within quotes followed by default-value "moni/Websphere/". Replace moni/Websphere/ with monitor/AMQ/ <monitor> <name>WebsphereMqMonitor</name> <type>managed</type> <argument... (4 Replies)
Discussion started by: prince1987
4 Replies

4. Shell Programming and Scripting

Search and replace the string with new word using xml tags

Hi All i need to replace the url1 inside <remote> tag in below xml in first instance and in the second instance with url2. any help appreciated <locations> <hudson.scm.SubversionSCM_-ModuleLocation> <remote>https://svn2015.com/svn/repos/internalshard</remote> ... (4 Replies)
Discussion started by: madankumar.t@hp
4 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

7. Shell Programming and Scripting

How can i use sed to replace a keyword in an xml file?

Hello Unix gurus! I'm a unix newbie. Can I use sed to replace a keyword in an xml file and convert this keyword with an output of a unix cat command? for example: <test>keyword</test> and temp.txt = hello! I would like to replace "keyword" with the output of "cat temp.txt". I think... (6 Replies)
Discussion started by: 4dirk1
6 Replies

8. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

9. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies
Login or Register to Ask a Question