开发者

BASH - How to replace only one tag in an XML, when duplicates appear?

开发者 https://www.devze.com 2023-04-05 21:24 出处:网络
Here is how my xml file looks like: <modelVersion>4.0.0</modelVersion> <version>5.1.3-SNAPSHOT</version>

Here is how my xml file looks like:

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

<parent>
    <artifactId>xxx&l开发者_C百科t;/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>.

How do I do that?


This is past the capabilities of simple line-based approaches. You might use sed magic with line selection operators or do 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):

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号