亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
1234下一頁(yè)
最近訪問(wèn)板塊 發(fā)新帖
查看: 52041 | 回復(fù): 30
打印 上一主題 下一主題

[文本處理] 使用sed和awk修改xml文件中的屬性值。 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2016-08-11 19:12 |只看該作者 |倒序?yàn)g覽
項(xiàng)目自動(dòng)部署tomcat,需要腳本自動(dòng)配置端口,需要修改如下格式的server.xml。

比如我要修改包含“scheme="https"”屬性的Connector節(jié)點(diǎn)中的port屬性值,請(qǐng)問(wèn)使用sed和awk怎么處理?
有一個(gè)問(wèn)題是:Connector節(jié)點(diǎn)信息分了好幾行,一個(gè)sed搞不定,如果全部在一行還可以使用sed命令進(jìn)行修改。

由于不懂python和perl腳本,之前在網(wǎng)上找了一個(gè)python腳本并沒(méi)有解決問(wèn)題,同時(shí)將配置文件的格式全部搞亂了。以前接觸過(guò)sed和awk可以解決這類(lèi)問(wèn)題的,還希望有大神幫忙看看怎么處理,謝謝了。!

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at


  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the BIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

論壇徽章:
28
15-16賽季CBA聯(lián)賽之八一
日期:2016-02-22 19:10:4215-16賽季CBA聯(lián)賽之深圳
日期:2016-12-01 10:34:0415-16賽季CBA聯(lián)賽之新疆
日期:2016-12-07 10:24:2915-16賽季CBA聯(lián)賽之同曦
日期:2016-12-15 12:06:43CU十四周年紀(jì)念徽章
日期:2016-12-18 13:03:4415-16賽季CBA聯(lián)賽之吉林
日期:2017-01-03 15:52:2515-16賽季CBA聯(lián)賽之遼寧
日期:2017-01-04 14:58:2415-16賽季CBA聯(lián)賽之遼寧
日期:2017-01-15 09:42:512016科比退役紀(jì)念章
日期:2017-02-06 17:21:50黑曼巴
日期:2017-02-10 15:46:1215-16賽季CBA聯(lián)賽之上海
日期:2017-03-18 10:14:5415-16賽季CBA聯(lián)賽之青島
日期:2017-03-18 22:00:44
2 [報(bào)告]
發(fā)表于 2016-08-11 19:34 |只看該作者
本帖最后由 moperyblue 于 2016-08-11 22:09 編輯
  1. sed -r '/<Connector/{:a;/\/>/!{N;ba};/scheme="https"/s/(port=")[^"]*/\18888/}' server.xml
復(fù)制代碼

論壇徽章:
4
程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2016-08-11 06:20:00數(shù)據(jù)庫(kù)技術(shù)版塊每日發(fā)帖之星
日期:2016-08-13 06:20:00程序設(shè)計(jì)版塊每日發(fā)帖之星
日期:2016-08-14 06:20:00數(shù)據(jù)庫(kù)技術(shù)版塊每日發(fā)帖之星
日期:2016-08-14 06:20:00
3 [報(bào)告]
發(fā)表于 2016-08-11 20:22 |只看該作者
本帖最后由 杰瑞26 于 2016-08-11 20:23 編輯

root@localhost:~/training# cat server.xml | grep -B 1 'scheme="https"' | sed -r 's:port="[0-9]+":port="8888":'
    <Connector port="8888" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"

論壇徽章:
30
申猴
日期:2014-04-10 09:43:532015年亞洲杯紀(jì)念徽章
日期:2015-03-20 14:40:232015亞冠之阿爾納斯?fàn)?日期:2015-06-02 18:59:042015亞冠之阿爾希拉爾
日期:2015-06-30 15:22:572015亞冠之大阪鋼巴
日期:2015-07-20 10:44:332015亞冠之阿爾納斯?fàn)?日期:2015-10-28 14:57:5215-16賽季CBA聯(lián)賽之新疆
日期:2015-12-25 10:18:45黑曼巴
日期:2016-06-26 21:39:5315-16賽季CBA聯(lián)賽之山西
日期:2016-07-25 21:54:2715-16賽季CBA聯(lián)賽之北京
日期:2016-10-27 12:07:2315-16賽季CBA聯(lián)賽之八一
日期:2017-07-07 16:39:0915-16賽季CBA聯(lián)賽之吉林
日期:2017-09-04 12:14:43
4 [報(bào)告]
發(fā)表于 2016-08-11 21:55 |只看該作者
  1. awk -vRS="-->" '/scheme="https"/{sub(/port="[0-9]+"/,"port=\"1111\"",$0)}{print $0RT}' file
復(fù)制代碼
回復(fù) 1# 哥哥_88


   

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2016-08-12 09:23 |只看該作者
回復(fù) 2# moperyblue


    非常感謝,這個(gè)寫(xiě)法是可以的,只不過(guò)約束我的server.xml格式必須是固定的,就是找到“scheme="https"”,然后往上找一行修改port的值。不過(guò)這也是比較接近的一種解決方法,非常感謝。!

[root@iZ11phehicpZ test]# cat server.xml
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
  </Service>
</Server>

[root@iZ11phehicpZ test]# sed -r '/<Connector/{:a;/\/>/!{N;ba};/scheme="https"/s/(port=")[^"]*/\118888/}' server.xml
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="18888" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
  </Service>
</Server>

論壇徽章:
39
辰龍
日期:2013-08-21 15:45:192015亞冠之廣州富力
日期:2015-05-12 16:34:52亥豬
日期:2015-03-03 17:22:00申猴
日期:2015-03-03 17:21:37未羊
日期:2014-10-10 13:45:41戌狗
日期:2014-06-17 09:53:29巨蟹座
日期:2014-06-12 23:17:17雙魚(yú)座
日期:2014-06-10 12:42:44寅虎
日期:2014-06-09 12:52:172015亞冠之卡爾希納薩夫
日期:2015-05-24 15:24:35黃金圣斗士
日期:2015-12-02 17:25:0815-16賽季CBA聯(lián)賽之吉林
日期:2017-06-24 16:43:52
6 [報(bào)告]
發(fā)表于 2016-08-12 09:23 |只看該作者
  1. awk '$2~/port=/{getline a;if(a~/scheme="https"/)gsub(/[0-9]+/,"8443",$2);$0=$0"\n"a}1' urfile
復(fù)制代碼

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2016-08-12 09:24 |只看該作者
回復(fù) 3# 杰瑞26


    這個(gè)語(yǔ)句和1樓的原理應(yīng)該類(lèi)似,但是至少將內(nèi)容cat出來(lái)了,我是想直接修改server.xml文件中的內(nèi)容。還是非常感謝!!

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2016-08-12 09:27 |只看該作者
回復(fù) 4# zxy877298415


    這個(gè)好像不行哎,效果如下:

[root@iZ11phehicpZ test]# cat server.xml
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
  </Service>
</Server>
[root@iZ11phehicpZ test]# awk -vRS="-->" '/scheme="https"/{sub(/port="[0-9]+"/,"port=\"18888\"",$0)}{print $0RT}' server.xml
<?xml version='1.0' encoding='utf-8'?>
<Server port="18888" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
  </Service>
</Server>

論壇徽章:
0
9 [報(bào)告]
發(fā)表于 2016-08-12 09:35 |只看該作者
回復(fù) 6# 關(guān)陰月飛


    感謝感謝,這個(gè)結(jié)果和1樓的一樣,一個(gè)用的sed,一個(gè)用的awk,這兩個(gè)命令的參數(shù)都太復(fù)雜了,看不懂。


server.xml文件如下:
<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
  </Service>
</Server>

我之前接觸過(guò)的一個(gè)類(lèi)似的腳本,他是一個(gè)復(fù)雜的方法實(shí)現(xiàn)的。好像步驟是這樣的

如果我要修改某一個(gè)節(jié)點(diǎn),只要傳入該節(jié)點(diǎn)的任意一個(gè)屬性,如“SSLEnabled="true"” 或者“sslProtocol="TLS"”,她都能找到對(duì)應(yīng)的節(jié)點(diǎn)將節(jié)點(diǎn)中你想修改的屬性值修改掉。

大致步驟類(lèi)似:
1、從文件的開(kāi)始截取到修改節(jié)點(diǎn)的起始位置,寫(xiě)入一個(gè)新文件。
2、截取當(dāng)前節(jié)點(diǎn)結(jié)束位置到文件的結(jié)束位置,然后拼接到那個(gè)新文件。
3、截取當(dāng)前節(jié)點(diǎn)修改內(nèi)容
4、最后將修改后的節(jié)點(diǎn)內(nèi)容塞到那個(gè)新文件中去。
完成xml文件的修改。


不知道我描述的對(duì)不對(duì),之前的實(shí)現(xiàn)好像是這樣的。這樣是最靈活的修改方式,而不是局限于xml格式修改。))

本人知道一點(diǎn)shell腳本,但是對(duì)sed和awk實(shí)在不是很會(huì)用。

論壇徽章:
0
10 [報(bào)告]
發(fā)表于 2016-08-12 09:38 |只看該作者
非常感謝大牛們的熱心回復(fù)!!!
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專(zhuān)區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP