Linux sed-多线查找和更换
我正试图使用sed从本质上取消对javatomcat的server.xml文件中的一段代码的注释。我在使用多行查找/替换来删除XML注释标记时遇到问题。以下是server.xml文件的一个片段:Linux sed-多线查找和更换,linux,sed,devops,Linux,Sed,Devops,我正试图使用sed从本质上取消对javatomcat的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 depen
<!-- 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'/不确定您的确切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>
-->
$ 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>
结果:
$ 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>
-->
$ 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>
$sed-nf foo.sed foo.xml
(用来详细了解它是如何工作的。)我最终用两种不同的SED得到了它:
## 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
##找到前两行并替换为一行以删除开始注释:
sed-z的//\n\n\n\n/'/opt/tomcat/conf/server.xml
您确定要使用sed之类的正则表达式工具来解析XML吗?不一定——我只是尝试使用现有容器中烘焙的内容,而不必加载perl/python之类的内容。我们正在努力使容器尽可能薄。你还有别的建议吗?答案字面上提到了Tomcata,答案删除了我只想删除一组的所有注释标记。我实现sed的方式几乎相同,我只是删除了所有的行开始符号(^),因为它们在我的server.xml中间隔了一点。它返回了整个文件,没有改变。