【问题标题】:sed - mult-line find and replacesed - 多行查找和替换
【发布时间】: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
  • 答案删除了我只想删除一组的所有评论标签。

标签: linux sed devops


【解决方案1】:

不确定您的确切 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 详细了解它的工作原理。)

【讨论】:

  • 我以几乎相同的方式实现了 sed,我只是删除了所有行开始符号 (^),因为它们在我的 server.xml 中有点间隔。它返回了整个文件,没有改变。
【解决方案2】:

我最终得到了两种不同的 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

【讨论】:

    猜你喜欢
    • 2021-10-06
    • 2020-06-24
    • 2020-02-03
    • 2020-08-05
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    相关资源
    最近更新 更多