我有以下正则表达式:
(<parent>(?s).*<version>).*(</version>(?s).*</parent>)
这应该适用于以下文本:
<name>CTR</name>
<!-- Parent -->
<parent>
<groupId>cxxdsds</groupId>
<artifactId>c222</artifactId>
<version>5.0.0-REPO</version>
</parent>
<scm>
我想替换<版本>并且<版本>。但我的sed不起作用:sed -i 's/(<parent>(?s).*<version>).*(</version>(?s).*</parent>)/1xxxxxxx2/g' pom.xml
有什么想法吗?
使用所示的示例,您可以尝试以下sed
代码进行替换。
sed 's/(<version>)[^<]*(<.*)/1xxxxxxx1/' Input_file
解释:简单的解释是,使用sed
的反向引用功能将<version>
和</version>
存储在两个不同的捕获组中,然后在执行替换时,根据所需输出在两个捕获组之间添加新值xxxxxxx
。
第二个解决方案:如果您想根据所示示例查找标记<parent>
,并且只想替换其中的版本,请使用awk
,然后尝试以下操作。
awk '
/<parent>/ { found=1 }
/<version>/{
line=$0
next
}
/</parent>/ && found{
if(line){
sub(/>.*</,">xxxxxxx<",line)
}
print line ORS $0
line=found=""
next
}
1
' Input_file