sed在匹配模式后添加xml行

时间:2014-07-24 07:20:06

标签: unix sed

我必须添加

<configuration>
      <jsp-configuration
       display-source-fragment="false"
       x-powered-by="false"/>
    </configuration>

匹配此图案线后

 <connector name="http" protocol="HTTP/1.1" scheme="http" socket-binding="http"/>

我如何使用sed命令实现这一点我尝试了一些像这样的

sed '/connector name="http" protocol="HTTP\/1.1" scheme="http" socket-binding="http"/a\ \<configuration>\n\<jsp-configuration display-source-fragment="false" x-powered-by="false"/>\n\</configuration> abc.xml

1 个答案:

答案 0 :(得分:1)

awk将执行:

awk '/<connector name="http" protocol="HTTP\/1.1" scheme="http" socket-binding="http"\/>/ {$0=$0"\n<configuration>\n      <jsp-configuration\n       display-source-fragment=\"false\"\n       x-powered-by=\"false\"/>\n    </configuration>"}1' abc.xml
<connector name="http" protocol="HTTP/1.1" scheme="http" socket-binding="http"/>
<configuration>
      <jsp-configuration
       display-source-fragment="false"
       x-powered-by="false"/>
    </configuration>

或使用sed

sed '/<connector name="http" protocol="HTTP\/1.1" scheme="http" socket-binding="http"\/>/a <configuration>\n      <jsp-configuration\n       display-source-fragment=\"false\"\n       x-powered-by=\"false\"/>\n    </configuration>' abc.xml
<connector name="http" protocol="HTTP/1.1" scheme="http" socket-binding="http"/>
<configuration>
      <jsp-configuration
       display-source-fragment="false"
       x-powered-by="false"/>
    </configuration>

你离sed很近,你最后错过'和一些空格。