1

I need to replace all the occurrences of a placeholder in a file. I'm using bash script for it.
I have tried the below command:

$Service_Name="ABC"
sed -i "s/_servicename_/$Service_Name/" XYZ.java

It only replaces the first word of a placeholder in a line in a file not multiple placeholders in a line.

Example:
If the line in the file is :

_servicename_WorkerException(String.format(_servicename_Test.Failed.getMessage()));

Now the output looks like this:

ABCWorkerException(String.format(_servicename_Test.Failed.getMessage()));

I want the below output:

ABCWorkerException(String.format(ABCTest.Failed.getMessage()));

Can someone help with the missing part?

Appreciate all your help! Thanks in advance!

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
Sweta Sharma
  • 2,404
  • 4
  • 21
  • 36

2 Answers2

3

You should change

sed -i "s/_servicename_/$Service_Name/" XYZ.java

in

sed -i "s/_servicename_/$Service_Name/g" XYZ.java

In other words, you have to add the g specifier.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
2

You are missing g flag

sed -i "s/_servicename_/$Service_Name/g" XYZ.java

g is used to apply the replacement to all matches to the regexp, not just the first.