2

I know this question has been asked before, I just can't seem to get the correct syntax for my sed command.

I need to replace OPP/com.user.opp.orchest.po.services.stub-npo/npo-stub with OPP/com.user.opp.orchest.po.services.stub-ica/npo-ica

A snippet of the file I am replacing it is the following:

config.xml

<compareType>PLAIN</compareType>
<pattern>
OPP/com.user.opp.orchest.po.services.stub-npo/npo-stub
</pattern>
<branches>
<com.sonyuser.hudson.plugins.gerrit.trigger.hudsontrigger.data.Branch>
<compareType>ANT</compareType>
<pattern>master</pattern>
</com.sonyuser.hudson.plugins.gerrit.trigger.hudsontrigger.data.Branch>
</branches>
${REPO_MIRROR}/OPP/com.user.opp.orchest.po.services.stub-npo/npo-stub

I have tried the following,

sed -i '/^\/OPP/\com.user.opp.orchest.po.services.stub-npo/\npo-stub\/OPP/\com.user.opp.orchest.po.services.stub-ica/\npo-ica/g' config.xml
user3292394
  • 609
  • 2
  • 11
  • 24

2 Answers2

0

You need to specify s command and replace the /\ with \/. There are some other typos here as well (\/ at the start is not necessary). Also, escape dots to match literal dots. A good idea is to use some other delimiter here instead of /, e.g. ,, because you have / chars in the regex and replacement parts.

You may use

sed -i 's,^OPP/com\.user\.opp\.orchest\.po\.services\.stub-npo/npo-stub,OPP/com.user.opp.orchest.po.services.stub-ica/npo-ica,' file

See the online demo

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

In your command, you are missing s for substitution and have wrongly escaped \ character. Also as you replied to my comment, that you want to replace it from anywhere in the file, you don't have to use ^ character in your regex. And dot . in regex means any character so they need to be escaped too.

You can use this command,

sed -i 's/OPP\/com\.user\.opp\.orchest\.po\.services\.stub-npo\/npo-stub/OPP\/com.user.opp.orchest.po.services.stub-ica\/npo-ica/g' yourfilename
Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36