免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 51270 | 回复: 30
打印 上一主题 下一主题

[文本处理] 使用sed和awk修改xml文件中的属性值。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-08-11 19:12 |只看该作者 |倒序浏览
项目自动部署tomcat,需要脚本自动配置端口,需要修改如下格式的server.xml。

比如我要修改包含“scheme="https"”属性的Connector节点中的port属性值,请问使用sed和awk怎么处理?
有一个问题是:Connector节点信息分了好几行,一个sed搞不定,如果全部在一行还可以使用sed命令进行修改。

由于不懂python和perl脚本,之前在网上找了一个python脚本并没有解决问题,同时将配置文件的格式全部搞乱了。以前接触过sed和awk可以解决这类问题的,还希望有大神帮忙看看怎么处理,谢谢了!!!

<?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联赛之八一
日期:2016-02-22 19:10:4215-16赛季CBA联赛之深圳
日期:2016-12-01 10:34:0415-16赛季CBA联赛之新疆
日期:2016-12-07 10:24:2915-16赛季CBA联赛之同曦
日期:2016-12-15 12:06:43CU十四周年纪念徽章
日期:2016-12-18 13:03:4415-16赛季CBA联赛之吉林
日期:2017-01-03 15:52:2515-16赛季CBA联赛之辽宁
日期:2017-01-04 14:58:2415-16赛季CBA联赛之辽宁
日期:2017-01-15 09:42:512016科比退役纪念章
日期:2017-02-06 17:21:50黑曼巴
日期:2017-02-10 15:46:1215-16赛季CBA联赛之上海
日期:2017-03-18 10:14:5415-16赛季CBA联赛之青岛
日期:2017-03-18 22:00:44
2 [报告]
发表于 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
复制代码

论坛徽章:
4
程序设计版块每日发帖之星
日期:2016-08-11 06:20:00数据库技术版块每日发帖之星
日期:2016-08-13 06:20:00程序设计版块每日发帖之星
日期:2016-08-14 06:20:00数据库技术版块每日发帖之星
日期:2016-08-14 06:20:00
3 [报告]
发表于 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年亚洲杯纪念徽章
日期:2015-03-20 14:40:232015亚冠之阿尔纳斯尔
日期:2015-06-02 18:59:042015亚冠之阿尔希拉尔
日期:2015-06-30 15:22:572015亚冠之大阪钢巴
日期:2015-07-20 10:44:332015亚冠之阿尔纳斯尔
日期:2015-10-28 14:57:5215-16赛季CBA联赛之新疆
日期:2015-12-25 10:18:45黑曼巴
日期:2016-06-26 21:39:5315-16赛季CBA联赛之山西
日期:2016-07-25 21:54:2715-16赛季CBA联赛之北京
日期:2016-10-27 12:07:2315-16赛季CBA联赛之八一
日期:2017-07-07 16:39:0915-16赛季CBA联赛之吉林
日期:2017-09-04 12:14:43
4 [报告]
发表于 2016-08-11 21:55 |只看该作者
  1. awk -vRS="-->" '/scheme="https"/{sub(/port="[0-9]+"/,"port=\"1111\"",$0)}{print $0RT}' file
复制代码
回复 1# 哥哥_88


   

论坛徽章:
0
5 [报告]
发表于 2016-08-12 09:23 |只看该作者
回复 2# moperyblue


    非常感谢,这个写法是可以的,只不过约束我的server.xml格式必须是固定的,就是找到“scheme="https"”,然后往上找一行修改port的值。不过这也是比较接近的一种解决方法,非常感谢!!!

[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双鱼座
日期: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联赛之吉林
日期:2017-06-24 16:43:52
6 [报告]
发表于 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
复制代码

论坛徽章:
0
7 [报告]
发表于 2016-08-12 09:24 |只看该作者
回复 3# 杰瑞26


    这个语句和1楼的原理应该类似,但是至少将内容cat出来了,我是想直接修改server.xml文件中的内容。还是非常感谢!!!

论坛徽章:
0
8 [报告]
发表于 2016-08-12 09:27 |只看该作者
回复 4# zxy877298415


    这个好像不行哎,效果如下:

[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 [报告]
发表于 2016-08-12 09:35 |只看该作者
回复 6# 关阴月飞


    感谢感谢,这个结果和1楼的一样,一个用的sed,一个用的awk,这两个命令的参数都太复杂了,看不懂。


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>

我之前接触过的一个类似的脚本,他是一个复杂的方法实现的。好像步骤是这样的

如果我要修改某一个节点,只要传入该节点的任意一个属性,如“SSLEnabled="true"” 或者“sslProtocol="TLS"”,她都能找到对应的节点将节点中你想修改的属性值修改掉。

大致步骤类似:
1、从文件的开始截取到修改节点的起始位置,写入一个新文件。
2、截取当前节点结束位置到文件的结束位置,然后拼接到那个新文件。
3、截取当前节点修改内容
4、最后将修改后的节点内容塞到那个新文件中去。
完成xml文件的修改。


不知道我描述的对不对,之前的实现好像是这样的。这样是最灵活的修改方式,而不是局限于xml格式修改。))

本人知道一点shell脚本,但是对sed和awk实在不是很会用。

论坛徽章:
0
10 [报告]
发表于 2016-08-12 09:38 |只看该作者
非常感谢大牛们的热心回复!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP