user11606914
user11606914

Reputation:

Replace String using Sed?

I am new to Unix and Linux and am trying to replace a certain strings in a file using sed.

This is what I have so far:

Param1=CHANGEME 
Param2=Value2 
Param3=CHANGEME 
Param4=Value4

If I do this command :- sed -i 's/CHANGEME/VALUE/g' input.txt, "CHANGEME" will be replaced by the text "VALUE" which is fine.

What if I want to change "Value" with it's corresponding value number so essentially for Param1=Value1 and for Param3=Value3 ?

How can I achieve that? Thank you

Upvotes: 0

Views: 4227

Answers (1)

tink
tink

Reputation: 15205

Using GNU sed ...

Your snippet saved to hugger:

cat hugger 
Param1=CHANGEME 
Param2=value2 
Param3=CHANGEME 
Param4=value

The following sed command does what you want, I think:

sed -r '/CHANGEME/s/Param([0-9]+)=.*/Param\1=Value\1/' hugger 
Param1=Value1
Param2=value2 
Param3=Value3
Param4=value4

Upvotes: 2

Related Questions