linux - 如何在一個字串後面追加字串?
淡淡烟草味
淡淡烟草味 2017-06-20 10:06:29
0
2
1479

各位大蝦好!

小蝦最近被開發提出一個需求:需要把所有含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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板