BASH - 当出现重复项时,如何仅替换XML中的一个标记?

时间:2021-01-15 21:05:46

Here is how my xml file looks like:

这是我的xml文件的样子:

    <modelVersion>4.0.0</modelVersion>
    <version>5.1.3-SNAPSHOT</version>

<parent>
    <artifactId>xxx</artifactId>
    <groupId>group</groupId>
    <version>5.1.3-SNAPSHOT</version>
</parent>

<artifactId>yyy</artifactId>
<packaging>pom</packaging>
<name>artifact name</name>

<modules>           
    <module>m1</module>
    <module>m2</module>
    <module>m3</module>
</modules>

I want to delete the "-SNAPSHOT" string ONLY from the tag within <parent>.

我想从 中的标签中删除“-SNAPSHOT”字符串。

How do I do that?

我怎么做?

1 个解决方案

#1


1  

This is past the capabilities of simple line-based approaches. You might use sed magic with line selection operators or do perl.

这超越了简单的基于线的方法的能力。您可以将sed magic用于行选择运算符或者使用perl。

An example in sed would be (but just for educational purposes! Don't try this at home! This script is pretty unstable towards changes in the XML):

sed中的一个例子是(但仅用于教育目的!不要在家里尝试这个!这个脚本对于XML的更改非常不稳定):

/<parent>/,/<.parent>/s/-SNAPSHOT//

However, XSLT and xsltproc is definitely the preferred solution for such heavy XML-processing duties.

但是,XSLT和xsltproc绝对是这种繁重的XML处理职责的首选解决方案。

#1


1  

This is past the capabilities of simple line-based approaches. You might use sed magic with line selection operators or do perl.

这超越了简单的基于线的方法的能力。您可以将sed magic用于行选择运算符或者使用perl。

An example in sed would be (but just for educational purposes! Don't try this at home! This script is pretty unstable towards changes in the XML):

sed中的一个例子是(但仅用于教育目的!不要在家里尝试这个!这个脚本对于XML的更改非常不稳定):

/<parent>/,/<.parent>/s/-SNAPSHOT//

However, XSLT and xsltproc is definitely the preferred solution for such heavy XML-processing duties.

但是,XSLT和xsltproc绝对是这种繁重的XML处理职责的首选解决方案。