Pergunta

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
Foi útil?

Solução

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)

Outras dicas

You could use xmlstarlet:

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

(Or something similar)

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.

Use a proper XML parser. E.g., here is what I would do in xsh:

open 1.xml ;
for //Console/node {
    if xsh:matches(., '[0-9][a-z]+') set text() 'newvalue' ;
}
save :b ;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top