Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace xml value with sed [duplicate]

Tags:

bash

xml

sed

I would like to replace value in XML file. Sample XML

<Console>
                <!-- REQUIRED PARAMETERS -->
                <!-- Enter the node that you are running the installation program from. This value must be a fully qualified name, which includes a host name and domain name. For example, node001.server.name.com.-->
                <node>node.sample.ibm.com</node>

                <!-- Enter the domain name of the cluster as a single dot-separated string. For example, server.name.com. -->
        <sso-domain-name>sample.ibm.com</sso-domain-name>

        <!-- Set this property to true if your InfoSphere BigInsights Console uses HTTPS. Otherwise, enter false. -->
                <https>false</https>

        <!-- management-console-port specifies which port is used by the Management Console. -->
        <management-console-port>8080</management-console-port>

I would like to replace value. I tried with following but its not working. Any help appreciated.

sed -i "/<Console>/,/<\/Console>/ s/<node>[0-9][a-z]\+/<node>newvalue
like image 974
Vinod Avatar asked May 09 '14 08:05

Vinod


3 Answers

sed -i -e '/<Console>/,/<\/Console>/ s|<node>[0-9a-z.]\{1,\}</node>|<node>newvalue</node>|g' YourFile

i assume value is between your tag and and only contain small letter, dot and digit (based on your sample and try). If extended, just add character missing in the class like [0-9a-z._-A-Z:] and maybe sub-class [:space:]

if newvalue is a variable content, dont forget to use double quote and escape character &, \ and the separator of sed s action (default is / and | in my code)

like image 150
NeronLeVelu Avatar answered Nov 03 '22 19:11

NeronLeVelu


You could use xmlstarlet:

xmlstarlet ed -u //Console/node -v 'new value'

(Or something similar)

like image 35
BeniBela Avatar answered Nov 03 '22 18:11

BeniBela


Simple XML can be handled with the xml2 suite quite well.

You can translate an XML into a line-based format and work on that like you are used to. i.e.:

xml2 < x.xml | perl -pe 's|^(/Console/node=)(.*)$|$1newvalue|g' | 2xml > y.xml

I am not so versed in sed I am sure you can replace perl with sed easily.

like image 1
towi Avatar answered Nov 03 '22 18:11

towi