[英]Replace multiline string by multiline string using perl
我正在嘗試使用perl編輯tomcat配置文件。 我想取消注釋xml文件中的以下行。 我在perl上嘗試過,但無法正常工作。
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->
這就是我得到的:
perl -i -pe 's~<!--\n <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"\n maxThreads="150" SSLEnabled="true" scheme="https" secure="true"\n clientAuth="false" sslProtocol="TLS" />\n -->~<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"\n maxThreads="150" SSLEnabled="true" scheme="https" secure="true"\n clientAuth="false" sslProtocol="TLS" />~' $CATALINA_HOME/conf/server.xml
另一個問題:我可以查找並用正則表達式替換字符串嗎?
謝謝你的幫助!
正則表達式不是修改XML的正確工具。 使用XML感知庫。 例如, XML :: LibXML :
#! /usr/bin/perl
use warnings;
use strict;
use XML::LibXML;
my $xml = 'XML::LibXML'->load_xml(location => 'file.xml');
my $comment = $xml->findnodes('//comment()')->[0];
my $inner = 'XML::LibXML'->load_xml(string => $comment->textContent)
->getFirstChild;
$comment->replaceNode($inner);
print $xml;
或更短一些的xsh包裝器:
open file.xml ;
xinsert chunk string(//comment()[1]) replace //comment()[1] ;
save :b ;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.