-1

There are mutiple port configuration in each XML block in a XML file.

The example below has two port configurations. One in <Server>, another in <Connector>.

I want to change port in <Connector> block only.

In this case, how can I solve this using sed command?

(I need a solution which only uses sed command because it should be run on any unix env.)

(I want to indicate specific block as name. It is not applicable way to change n-th block for matching port text. Because there are more port configurations as the comment in a file.)

server.xml

<?xml version='1.0' encoding='UTF-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Service name="Catalina">
    <Connector port="80" protocol="HTTP/1.1"/>
    <Engine name="Catalina" defaultHost="localhost">
      <Host name="localhost" appBase="webapps"/>
    </Engine>
  </Service>
</Server>
Yoonsung Lee
  • 115
  • 1
  • 9
  • 2
    [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Nov 05 '20 at 05:55
  • @Cyrus It is reasonable. I will find the way of using python. Thanks a lot. – Yoonsung Lee Nov 05 '20 at 07:26

1 Answers1

0

It's not safe at all to replace strings in xml files. You can break syntax, xml structure and it won't permit clever operation like editing properties on node before / after selector and so on...

As @Cyrus suggested : use an xml parser. No matter it is an unix environment. Feel free to make an python or java script or your preferred language for that.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gweltaz Niquel
  • 629
  • 4
  • 14