0

各位大虾好!

小虾最近被开发提出一个需求:需要把所有含tomcat的模块里的server.xml文件里添加上对应服务器的内网IP,举个例子如下:

原有的server.xml的节选如下:

    <Service name="LMS">
            <Connector port="8080" connectionTimeout="20000" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"  enableLookups="false" disableUploadTimeout="true" maxThreads="500" minSpareThreads="20" acceptCount="100"/>

             <Connector port="8088" connectionTimeout="20000" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"  enableLookups="false" disableUploadTimeout="true" maxThreads="500" minSpareThreads="20" acceptCount="100"/>
                    
            <Connector port="8099" protocol="AJP/1.3" redirectPort="8443" />
            <Engine defaultHost="localhost" name="Catalina">
                     <Realm className="org.apache.catalina.realm.LockOutRealm">
                                    <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
                     </Realm>

现在要把<Connector port="8099" protocol="AJP/1.3" redirectPort="8443" /> 这一句里面加上内网IP,改成这样:

<Service name="LMS">
                <Connector port="8080" connectionTimeout="20000" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"  enableLookups="false" disableUploadTimeout="true" maxThreads="500" minSpareThreads="20" acceptCount="100"/>

                 <Connector port="8088" connectionTimeout="20000" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"  enableLookups="false" disableUploadTimeout="true" maxThreads="500" minSpareThreads="20" acceptCount="100"/>
                        
                <Connector port="8099" address="1.2.3.4" protocol="AJP/1.3" redirectPort="8443" />
                <Engine defaultHost="localhost" name="Catalina">
                         <Realm className="org.apache.catalina.realm.LockOutRealm">
                                        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase" />
                         </Realm>

请问怎么做?

我用sed -i '/^<Connector port="8099"/s/port="8099"/port="8099" address="1.2.3.4"/g' server.xml 这个命令发现无法匹配,请问这时候我该怎么办?

2个回答

0

已采纳
sed -i '/<Connector port="8099"/s/port="8099"/port="8099" address="1.2.3.4"/g' server.xml

你的匹配写错了,不要^.

1
回复 BruceWayne

^\s\+可以匹配行首为空白.

zhenguoli · 6 天前

展开评论
0

可以这样:

sed -i 's@Connector port="8099"@& address="1.2.3.4"@' server.xml

撰写答案