[英]Bash pattern match over multiple lines in pom.xml
I'm trying to edit my pom.xml file with a script. 我正在尝试使用脚本编辑pom.xml文件。 It involves inserting a plugin module after one that I expect to exist.
它涉及在我希望存在的插件模块之后插入一个插件模块。
My reduced pom looks something like this: 我减少的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>
I want to use a script to add another plugin after the zeroturnaround one. 我想使用脚本在零周转后添加另一个插件。 So basically I am looking for this pattern:
所以基本上我正在寻找这种模式:
<rootPath>$${webapp.jrebel.root}</rootPath>
</configuration>
</plugin>
And would like to insert something after this pattern. 并希望在此模式之后插入一些内容。 So the output should be
所以输出应该是
<rootPath>$${webapp.jrebel.root}</rootPath>
</configuration>
</plugin>
Something new here
sed doesn't work because the inputs come in line by line. sed不起作用,因为输入是逐行输入的。 So this
所以这
sed '/<rootPath>\$\${webapp.jrebel.root}<\/rootPath>/a Something new here' pom.xml
prints out 打印出来
<rootPath>$${webapp.jrebel.root}</rootPath>
Something new here
</configuration>
</plugin>
I have tried 我努力了
sed -i -e '/<rootPath>\$\${webapp.jrebel.root}<\/rootPath>/ {
N; /\n<\/configuration>/ {
N; /\n<\/plugin>/ {
s/<\/plugin>/<\/plugin>hello/
}
}
}' pom.xml
But that does nothing. 但这无济于事。
How can I pattern match this? 我该如何模式匹配? I am open to using either sed or awk.
我愿意使用sed或awk。
使用xmlstarlet
,您会说:
xmlstarlet ed -a //plugin -t elem -n whatever -s //whatever -t elem -n stuff pom.xml
Using XML tools to manipulate XML is good advice. 使用XML工具来操纵XML是一个很好的建议。 Havign said that, using GNU awk for multi-char RS, this may be adequate for your needs:
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
Here's the syntax if you don't have the "old" string in a file but just want to hard-code it in an awk variable instead: 如果文件中没有“旧”字符串,而只想将其硬编码在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
With other awks you'd build up the strings line by line then make the change in the END section: 使用其他awk,您将逐行构建字符串,然后在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
Not ideal, but, if you insist in using sed, you can try something like this : 不理想,但是,如果您坚持使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.