【发布时间】:2021-03-19 23:46:47
【问题描述】:
我正在尝试使用 sed 从本质上取消注释 Java Tomcat 的 server.xml 文件中的一段代码。我在使用多行查找/替换来删除 XML 注释标签时遇到问题。这是server.xml文件的sn-p:
<!-- 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 的换行让它工作了,但我不想走这条路,因为以后对文件的任何修改都会改变这个操作。
【问题讨论】:
-
您确定要使用像 sed 这样的正则表达式工具来解析 XML 吗?
-
不一定——我只是想使用我现有容器中的内容,而不是加载像 perl/python 这样的东西。我们正在努力使容器尽可能纤薄。您还有其他建议吗?
-
stackoverflow.com/questions/1464697/… 答案字面上提到了tomcat
-
答案删除了我只想删除一组的所有评论标签。