linux - 如何在一个字符串后面追加字符串?
淡淡烟草味
淡淡烟草味 2017-06-20 10:06:29
0
2
1495

各位大虾好!

小虾最近被开发提出一个需求:需要把所有含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)
滿天的星座
sed -i '/<Connector port="8099"/s/port="8099"/port="8099" address="1.2.3.4"/g' server.xml

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

大家讲道理

可以这样:

sed -i 's@Connector port="8099"@& address="1.2.3.4"@' server.xml
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板