如何使用sed(Jenkinsfile)在我的xml文件的close标签上方插入文本?

问题描述 投票:0回答:2

我有changelog_master.xml如下:

<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
   http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

  <include file="changelog/version/changelog_1.0.xml"/>

</databaseChangeLog>

我想在我的文件的close标签上面插入文字,就像这样

 <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
   http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

  <include file="changelog/version/changelog_1.0.xml"/>
  <include file="changelog/version/changelog_1.1.xml"/>

</databaseChangeLog>

我尝试使用这样的sed,因为我需要在Jenkinsfile中编写它以实现自动化

sh "sed -i '/<\/databaseChangelog>/i <include file=\"changelog/version/changelog_${version}.xml\"/>' changelog/changelog_master.xml"

但它不起作用。我也尝试了很多相关的问题。

我现在应该怎么做?

谢谢!

xml sed jenkins-pipeline xml-namespaces
2个回答
0
投票

添加子节点并在带有名称空间的XML文件中插入带有xmlstarlet的属性:

xmlstarlet edit \
  --omit-decl -N x="http://www.liquibase.org/xml/ns/dbchangelog" \
  --subnode "//x:databaseChangeLog" -t elem -n "include" \
  --insert "//x:databaseChangeLog/include" -t attr -n "file" --value "changelog/version/changelog_1.1.xml" file.xml

输出到标准输出:

<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog    http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
  <include file="changelog/version/changelog_1.0.xml"/>
  <include file="changelog/version/changelog_1.1.xml"/>
</databaseChangeLog>

如果要在其中编辑文件,请添加选项-L。

见:xmlstarlet edit --help


0
投票

sed也有效,这是我的版本(你的匹配模板中有拼写错误)

 $ sed -i "/<\/databaseChangeLog>/i\<include file=\"changelog/version/changelog_${version}.xml\"/>" changelog/changelog_master.xml
© www.soinside.com 2019 - 2024. All rights reserved.