Home > Net >  Replace a value of parameter in a file
Replace a value of parameter in a file

Time:05-24

I have a multiple files named paramaters.txt containing different parameters and their values:

param1 = vaue1
param2 = value2
param3 = value3

And I'd like to replace value of param2 and have

param1 = vaue1
param2 = value4
param3 = value3

I was trying to use sed but I was able to do it if I know the string I want to replace, value2 in my case. But I don't know it (or rather it can be different in different files). I would rather try to find string param2 and replace everything that is on the right side of it. But I don't know how to do it.

Any hint will be appreciated.

CodePudding user response:

An easy way to accomplish what you want is to use sed to find the line with the parameter you want and replace the entire line. Something like this should work:

sed -i 's/^param2 =.*/param2 = value4/' file
  • Related