我试图使用sed to来取消Java Tomcat的server.xml文件中的一段代码的注释。我在使用多行查找/替换来删除XML注释标记时遇到了麻烦。下面是server.xml文件的一个片段:
<!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443
This connector uses the NIO implementation. The default
SSLImplementation will depend on the presence of the APR/native
library and the useOpenSSL attribute of the
AprLifecycleListener.
Either JSSE or OpenSSL style configuration may be used regardless of
the SSLImplementation selected. JSSE style configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true">
<SSLHostConfig>
<Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
<!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
This connector uses the APR/native implementation which always uses
OpenSSL for TLS.
Either JSSE or OpenSSL style configuration may be used. OpenSSL style
configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" >
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
certificateFile="conf/localhost-rsa-cert.pem"
certificateChainFile="conf/localhost-rsa-chain.pem"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<!--
<Connector protocol="AJP/1.3"
address="::1"
port="8009"
redirectPort="8443" />
-->
,下面是我尝试执行查找/替换命令的一个例子:
sed '/ <!--n <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol/i test' /opt/tomcat/conf/server.xml
sed '/<!--n<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol/i test' /opt/tomcat/conf/server.xml
sed '/*<!--n*<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol/i test' /opt/tomcat/conf/server.xml
因为有许多注释标签,我必须将它与我试图取消注释的块关联起来,这就是为什么我试图做多行。
如果有人有任何关于如何使用sed或其他方法来完成此工作的指导,我将不胜感激。
TIA !
EDIT1:我已经通过使用sed的行替换使其工作,但我宁愿不走那条路,因为将来对文件的任何修改都会改变这个操作。
不确定您确切的xml文件格式。给你举个例子供参考。
示例XML文件:
$ cat foo.xml
<!-- real
comment
-->
<!--
<foo a="1"
uncomment="yes"
b="2" />
</foo>
-->
<!--
<foo a="1"
uncomment="no"
b="2" />
</foo>
-->
<!--
<foo a="1"
uncomment="yes"
b="2" />
</foo>
-->
sed
脚本:
$ cat foo.sed
/^<!--$/! {
p; b
}
h
:a
n; H
/^-->$/! ba
x
/uncomment="yes"/ {
s/^<!--n//
s/n-->$//
}
p
结果:
$ sed -nf foo.sed foo.xml
<!-- real
comment
-->
<foo a="1"
uncomment="yes"
b="2" />
</foo>
<!--
<foo a="1"
uncomment="no"
b="2" />
</foo>
-->
<foo a="1"
uncomment="yes"
b="2" />
</foo>
(使用sedsed查看其详细工作原理)
我最终得到了两个不同的种子:
## find the first 2 lines and replace with one to remove the start-comment:
sed -z 's/ <!--n <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"/ <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"/' /opt/tomcat/conf/server.xml
## find the whole block (had to make sure I was removing the right closing-comment on the right block) and replace with the whole block, with modifications included, and comment removed:
sed -z 's/ <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"n maxThreads="150" SSLEnabled="true">n <SSLHostConfig>n <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"n type="RSA" />n </SSLHostConfig>n </Connector>n -->/ <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"n maxThreads="150" SSLEnabled="true">n <SSLHostConfig>n <Certificate certificateKeystoreFile="conf/tomcat.p12"n certificateKeystorePassword="soopersecurepasswordtest"n type="RSA" />n </SSLHostConfig>n </Connector>/' /opt/tomcat/conf/server.xml