0


I'm trying to replace a multi-line text which contains XML in all files named pom.xml. In practice, I need to replace this section:

<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>

with:

<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>

I've come up with the following sed:

find . -name pom.xml -exec sed -i 's/\<groupId\>javax\.enterprise\<\/groupId\>
      \<artifactId\>cdi-api\<\/artifactId\>/\<groupId\>jakarta\.enterprise\<\/groupId\>
        \<artifactId\>jakarta\.enterprise\.cdi-api\<\/artifactId\>/g' {} + 

However, from the output it's returned "command not terminated". I'm not too familiar with multiline replacement, could you suggest any fix or other tool to do it? Thanks

Francesco Marchioni
  • 4,091
  • 1
  • 25
  • 40
  • 3
    [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Nov 01 '19 at 14:56
  • Besides what @Cyrus said, instead of using literal newlines you should have used `\n` and specified `-z` for `sed`. – alfunx Nov 01 '19 at 15:01

1 Answers1

0

First test without -i in a folder with one file.
Newer versions of GNU sed support the option -z.

find . -name pom.xml -exec \
   sed -rz 's#(<groupId>javax.enterprise</groupId>\n<artifactId>)(cdi-api</artifactId>)#\1jakarta.enterprise.\2#g' {} \;
Walter A
  • 19,067
  • 2
  • 23
  • 43
  • Thanks, I've tried it but it didn't have any effect on the file. I was trying with an online exp parser but it fails to capture text which is after the first line (`javax.enterprise`) even through I have used "\r" to separate lines – Francesco Marchioni Nov 01 '19 at 18:42
  • When you have a windows environment, you might have `\r\n`, so try to replace the string with `...\r\n...` – Walter A Nov 01 '19 at 22:54