2011-03-02 62 views
0

使用sed,我要搜索的字符串和替换,与其他中看到maven pom.xml如何忽略文本搜索中的换行符?

<groupId>com.abc</groupId> 
<version>3.1</version> 

<groupId>com.abc</groupId> 
<version>4.1</version> 

我写

find . -name 'pom.xml' -type f -exec sed -i "s!"<groupId>com.abc</groupId>\n 
<version>3.1</version>"!"<groupId>com.abc</groupId>\n <version>4.1</version>!" '{}' \; 

但它永远不会奏效!

+1

'sed'不是多行编辑的最佳工具。尝试使用'perl'来代替。 – mouviciel 2011-03-02 15:56:40

回答

2

您需要使用N命令来读取下一行。请参阅优秀的Sed and Awk书籍以获取有关处理多行图案空间的更多详细信息。这有点费劲。

保存此sed命令将文件e.g cmd.txt

/<groupId>com.abc<\/groupId>/{ 
N 
s/<groupId>com.abc<\/groupId>\n<version>3.1<\/version>/<groupId>com.abc<\/groupId>\n<version>4.1<\/version>/ 
} 

然后运行:

$ sed -f cmd.txt pom.xml 
0

试试这个......希望它有帮助。

sed -e 's/3.1/4.1/g' pom.xml > pom1.xml 
+0

由于存在多个版本为3.1的依赖项,因此无法使用此功能。需要搜索组ID以及 – Javabeginner 2011-03-02 16:07:50

+0

在这种情况下,您将不得不使用Perl作为@mouviviel提到的具有多种模式的多行搜索。 – Rahul 2011-03-02 16:14:40

1

请勿为此使用sed。 linebreaks是sed的一部分。这是xslt的工作。

下面的代码片段被借用和调整从http://chrisdail.com/2009/05/17/changing-maven-pom-versions-with-groovy-and-xslt/

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="version[../groupId='com.abc']"> 
     <version>4.1</version> 
    </xsl:template> 
</xsl:stylesheet> 

只是做xsltproc的thisfile.xsl的pom.xml