Search code examples
xmlbashsed

replace xml value with 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

Solution

  • 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)