开发者

how to ignore line break in text search?

开发者 https://www.devze.com 2023-02-14 14:30 出处:网络
Using sed, I want to search a string and replace that with other in maven pom.xml <groupId>com.abc</groupId>

Using sed, I want to search a string and replace that with other in maven pom.xml

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

with

<groupId>com.abc</groupId>
<vers开发者_开发知识库ion>4.1</version>

I wrote

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>!" '{}' \; 

But it never works!


You need to use the N command to read the next line. Take a look at the excellent Sed and Awk book for more details on handling multiline pattern spaces. It is a bit fiddly.

Save this sed command to a file 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>/
}

Then run:

$ sed -f cmd.txt pom.xml


Don't use sed for this. linebreaks are part of who sed is. This is a job for xslt.

The following code snippet was borrowed and tweaked from 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>

Just do xsltproc thisfile.xsl pom.xml


Try this ... Hope it helps.

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

精彩评论

暂无评论...
验证码 换一张
取 消