搜尋

首頁  >  問答  >  主體

linux - 如何在一個字串後面追加字串?

各位大蝦好!

小蝦最近被開發提出一個需求:需要把所有含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 這個指令發現無法匹配,請問這時候我該怎麼辦?

淡淡烟草味淡淡烟草味2724 天前1470

全部回覆(2)我來回復

  • 滿天的星座

    滿天的星座2017-06-20 10:08:29

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

    你的配對寫錯了,不要^.

    回覆
    0
  • 大家讲道理

    大家讲道理2017-06-20 10:08:29

    可以這樣:

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

    回覆
    0
  • 取消回覆