xmlstarlet replace value after pattern

2019-03-05 12:02发布

I have a pom.xml, that I want to change the properties values for tags that starting in a certain pattern.

I usually use xmlstarlet to manipulate XML but I never did it with "regex", Is that possible?

my pom.xml is such:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company.vector</groupId>
    <artifactId>company-vector</artifactId>
    <version>1.16-SNAPSHOT</version>
    <packaging>pom</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <modules>
        <module>company-vector-adapters-solaredge</module>
        <module>company-vector-topology-datasource-icp</module>
    </modules>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-collections4</artifactId>
                <version>4.1</version>
            </dependency>
        </dependencies>
    </dependencyManagement>  
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                </plugin>
        </pluginManagement>
    </build>
    <properties>
        <repository.snapshots.uri>repository/maven-snapshots</repository.snapshots.uri>
        <company.mgrid.common.version>0.8-SNAPSHOT</company.mgrid.common.version>
        <company.mgrid.infra.version>0.8-SNAPSHOT</company.mgrid.infra.version>
        <company.mgrid.globals.version>0.8-SNAPSHOT</company.mgrid.globals.version>
        <java.jdk.version>1.8</java.jdk.version>
    </properties>
</project>

And I need to replace values in all tags under node that starts with company.mgrid* from 0.8-SNAPSHOT to 0.9-SNAPSHOT,

So the output will be :

<properties>
    <repository.snapshots.uri>repository/maven-snapshots</repository.snapshots.uri>
    <company.mgrid.common.version>0.9-SNAPSHOT</company.mgrid.common.version>
    <company.mgrid.infra.version>0.9-SNAPSHOT</company.mgrid.infra.version>
    <company.mgrid.globals.version>0.9-SNAPSHOT</company.mgrid.globals.version>
    <java.jdk.version>1.8</java.jdk.version>
</properties>

As mentioned I prefer to do it with xmlstarlet but if you have a solution with sed or any other bash tool it will be appreciated. Help anyone?

2条回答
在下西门庆
2楼-- · 2019-03-05 12:43

I don't have xmlstartet but I can help with sed

sed -ibak 's/\(<company\.mgrid\.[^>]*>\)0.8-SNAPSHOT/\10.9-SNAPSHOT/' pom.xml

will do the job and backup your original file in pom.xml.bak if needed. I you dont want this backup file, remove bak after the i flag.

查看更多
Deceive 欺骗
3楼-- · 2019-03-05 12:45

What you are interested in is the Xpath function starts-with. It is a standard function since Xpath 1.0. This allows you to do :

$ xmlstarlet ed -N N="http://maven.apache.org/POM/4.0.0"        \
                -u "//N:*[starts-with(name(),'company.mgrid')]" \
                -v "0.9-SNAPSHOT" pom.xml
查看更多
登录 后发表回答