Bash模式匹配pom.xml中的多行

时间:2016-06-08 12:40:58

标签: bash awk sed

我正在尝试使用脚本编辑我的pom.xml文件。它涉及在我期望存在的插件模块之后插入插件模块。

我减少的pom看起来像这样:

<?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>very</groupId>
    <artifactId>secret</artifactId>
    <version>2.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>Something</name>

    <properties>
    ...
    </properties>

    <modules>
        <module>...</module>
    </modules>

    <prerequisites>
        ...
    </prerequisites>

    <profiles>
        <profile>
        ...
        </profile>
    </profiles>

    <dependencyManagement>
        <dependencies>
            <dependency>
                ...
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                ... 
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
            ...
            </plugin>
            <plugin>
                <groupId>org.zeroturnaround</groupId>
                <artifactId>jrebel-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>Generate JRebel configuration</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <relativePath>${relativeRoot}</relativePath>
                    <rootPath>$${webapp.jrebel.root}</rootPath>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <reporting>
        <plugins>
            <plugin>
            ...
            </plugin>
        </plugins>
    </reporting>

</project>

我想使用脚本在zeroturnaround之后添加另一个插件。所以基本上我正在寻找这种模式:

                <rootPath>$${webapp.jrebel.root}</rootPath>
            </configuration>
        </plugin>

并希望在此模式之后插入一些内容。所以输出应该是

                <rootPath>$${webapp.jrebel.root}</rootPath>
            </configuration>
        </plugin>
        Something new here

sed不起作用,因为输入逐行排列。所以这个

sed '/<rootPath>\$\${webapp.jrebel.root}<\/rootPath>/a Something new here' pom.xml

打印出来

               <rootPath>$${webapp.jrebel.root}</rootPath>
      Something new here
            </configuration>
        </plugin>

我试过了

sed -i -e '/<rootPath>\$\${webapp.jrebel.root}<\/rootPath>/ {
N; /\n<\/configuration>/ {
N; /\n<\/plugin>/ {
s/<\/plugin>/<\/plugin>hello/
}
}
}' pom.xml

但这没有任何作用。

我如何模式匹配?我愿意使用sed或awk。

3 个答案:

答案 0 :(得分:2)

使用xmlstarlet,您会说:

 xmlstarlet ed -a //plugin -t elem -n whatever -s //whatever -t elem -n stuff pom.xml

答案 1 :(得分:1)

使用XML工具来操作XML是一个很好的建议。 Havign说,使用GNU awk进行多字符RS,这可能足以满足您的需求:

$ cat file1
                <rootPath>$${webapp.jrebel.root}</rootPath>
            </configuration>
        </plugin>

$ cat file2
foo
                <rootPath>$${webapp.jrebel.root}</rootPath>
            </configuration>
        </plugin>
bar

$ awk -v RS='^$' -v ORS= -v new='Something new here' '
NR==FNR { old=$0; lgth=length(old); next }
start=index($0,old) {
    $0=substr($0,1,start+lgth-1) "\t" new "\n" substr($0,start+lgth)
}
1' file1 file2
foo
                <rootPath>$${webapp.jrebel.root}</rootPath>
            </configuration>
        </plugin>
        Something new here
bar

如果您没有&#34; old&#34;这里有语法。文件中的字符串,但只是想在awk变量中硬编码:

$ awk -v RS='^$' -v ORS=  \
-v old='
                <rootPath>$${webapp.jrebel.root}</rootPath>
            </configuration>
        </plugin>
' \
-v new='Something new here' '
start=index($0,old) {
    lgth=length(old)
    $0=substr($0,1,start+lgth-1) "\t" new "\n" substr($0,start+lgth)
}
1' file2
foo
                <rootPath>$${webapp.jrebel.root}</rootPath>
            </configuration>
        </plugin>
        Something new here
bar

使用其他awks,您可以逐行构建字符串,然后在END部分进行更改:

awk -v ORS= -v new='Something new here' '
NR==FNR { old = old $0 RS; next }
{ xml = xml $0 RS }
END {
    if ( start=index(xml,old) ) {
        lgth=length(old) 
        xml=substr(xml,1,start+lgth-1) "\t" new "\n" substr(xml,start+lgth)
    }
    print xml
}
' file1 file2
foo
                <rootPath>$${webapp.jrebel.root}</rootPath>
            </configuration>
        </plugin>
        Something new here
bar

答案 2 :(得分:-1)

不理想,但是,如果你坚持使用sed,你可以尝试这样的事情:

#!/bin/bash
for linenumber in `sed -n '/webapp.jrebel.root/=' pom.xml`
do
    sed -n $linenumber','$(($linenumber + 3))'p' pom.xml > tmpfile
    if [[ `sed -n  '/<\/configuration>/=' tmpfile` == 2 && `sed -n  '/<\/plugin>/=' tmpfile` == 3 ]]
    then
        sed -i $(($linenumber + 3))'i\Something new here\n' pom.xml
    fi
done