SED多行搜索和替换错误

时间:2018-07-14 06:44:02

标签: xml bash shell sed

我有一个如下的XML文件(config.xml)。

<?xml version="1.1" encoding="UTF-8"?>
  <project>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
   <triggers>
    <com.cloudbees.jenkins.GitHubPushTrigger plugin="github@1.29.2">
      <spec/>
    </com.cloudbees.jenkins.GitHubPushTrigger>
   </triggers>
   <concurrentBuild>false</concurrentBuild>
  </project>

我只想用<triggers/>替换触发器块。但是,当我使用SED执行此操作时,出现此错误:

sed: 1: "/<triggers>/{:a;N;/<\/t ...": unexpected EOF (pending }'s)

命令:

sed -i '.bak' '/<triggers>/{:a;N;/<\/triggers>/!ba;N;s/.*\n/<triggers\/>\n/};p' config.yml

我想让您知道我在这里想念的是什么以及如何获得理想的结果吗?

所需的输出:

<?xml version="1.1" encoding="UTF-8"?>
<project>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
</triggers>
<concurrentBuild>false</concurrentBuild>
</project>

1 个答案:

答案 0 :(得分:2)

使用此有效的XML文件:

<?xml version="1.0"?>
<root>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers>
    <com.cloudbees.jenkins.GitHubPushTrigger plugin="github@1.29.2">
      <spec/>
    </com.cloudbees.jenkins.GitHubPushTrigger>
  </triggers>
  <concurrentBuild>false</concurrentBuild>
</root>

使用此命令:

xmlstarlet edit --omit-decl --update "//triggers" --value ""

输出:

<root>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
</root>

如果要就地编辑文件,请添加选项-L。


请参阅:xmlstarlet edit --help

相关问题