免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 哥哥_88
打印 上一主题 下一主题

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

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
11 [报告]
发表于 2016-08-12 09:51 |只看该作者
回复 8# 哥哥_88

How about this way

$ perl -0 -pe '{s/(<Connector port=")\d+("[^>]+?scheme="https")/${1}18888${2}/gm}' server.xml > server_new.xml

$ diff server.xml server_new.xml
86c86
<     <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
---
>     <Connector port="18888" protocol="org.apache.coyote.http11.Http11Protocol"

$ grep -nB1 scheme server*.xml
server_new.xml-86-    <Connector port="18888" protocol="org.apache.coyote.http11.Http11Protocol"
server_new.xml:87:               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
--
server.xml-86-    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
server.xml:87:               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"

   

论坛徽章:
0
12 [报告]
发表于 2016-08-12 10:01 |只看该作者
回复 11# jason680


    感谢,虽然不懂perl脚本,但是感觉已经接近实现了。这个语句是修改port属性,如果我想修改<Connector>节点中的其他属性(如maxThreads,不限于该属性)该怎么写?盼回复,谢谢!!!

论坛徽章:
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
13 [报告]
发表于 2016-08-12 10:03 |只看该作者
  1. awk -vRS="/>" '/scheme="https"/{sub(/port="[0-9]+"/,"port=\"18888\"",$0)}{printf $0RT}' file
复制代码
回复 8# 哥哥_88


   

论坛徽章:
0
14 [报告]
发表于 2016-08-12 10:21 |只看该作者
回复 13# 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="/>" '/maxThreads="150"/{sub(/clientAuth="[^ \f\n\r\t\v]*"/,"clientAuth=\"true\"",$0)}{printf $0RT}' 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="true" sslProtocol="TLS" />
  </Service>
</Server>
[root@iZ11phehicpZ test]# awk -vRS="/>" '/sslProtocol="TLS"/{sub(/port="[^ \f\n\r\t\v]*"/,"port=\"18888\"",$0)}{printf $0RT}' 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>
[root@iZ11phehicpZ test]#

论坛徽章:
6
羊年新春福章
日期:2015-03-03 17:16:28双子座
日期:2015-03-03 17:16:56巳蛇
日期:2015-03-03 17:17:2415-16赛季CBA联赛之福建
日期:2016-03-11 09:05:00黑曼巴
日期:2016-07-07 16:58:1215-16赛季CBA联赛之吉林
日期:2016-11-14 09:23:07
15 [报告]
发表于 2016-08-12 10:44 |只看该作者
修改https端口号,还有注意修改redirectPort端口号
这个一般自动化部署会用j2模板,然后通过变量来替换

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
16 [报告]
发表于 2016-08-12 11:02 |只看该作者
回复 14# 哥哥_88

here you are

$ perl -076 -pe '{if(m/<Connector / && m/maxThreads="150"/){s/clientAuth="\K[^"]+/true/}}' server.xml > server_new.xml

$ diff server.xml server_new.xml
88c88
<                clientAuth="false" sslProtocol="TLS" />
---
>                clientAuth="true" sslProtocol="TLS" />

$ sed -n '86,88p' server.xml
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />

$ sed -n '86,88p' server_new.xml
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="true" sslProtocol="TLS" />

   

论坛徽章:
0
17 [报告]
发表于 2016-08-12 11:16 |只看该作者
回复 15# jcdiy0601


    感谢指正,请问j2模板是什么?怎么用?

    新手表示在摸索中,遇到问题解决问题的状态。

论坛徽章:
0
18 [报告]
发表于 2016-08-12 11:19 |只看该作者
回复 16# jason680


    非常感谢,同样完美解决当前问题。谢谢大牛!!!

论坛徽章:
2
极客徽章
日期:2016-12-07 14:05:2315-16赛季CBA联赛之新疆
日期:2017-02-06 17:31:41
19 [报告]
发表于 2016-08-12 11:24 |只看该作者
本帖最后由 butterflyswim 于 2016-08-12 11:33 编辑
  1. grep -B 1 'scheme="https"' file |sed 's/8443/8888/'
复制代码

论坛徽章:
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
20 [报告]
发表于 2016-08-12 11:30 |只看该作者
回复 12# 哥哥_88


    一样的思路
  1. sed -r '/<Connector/{:a;/\/>/!{N;ba};/scheme="https"/s/(maxThreads=")[^"]*/\1100/}' server.xml
  2. sed -r '/<Connector/{:a;/\/>/!{N;ba};/scheme="https"/s/(clientAuth=")[^"]*/\1true/}' server.xml
复制代码
关键字这些可以在shell脚本中可以提取为变量
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP