Search code examples
sed

Linux sed command to replace dynamic values


I'd like to replace several string in a file having a 'false' value with a 'true' value with a single 'sed' command.

My problem is that in the middle of the string there is a dynamic part like one,two,three,[..] that I want to maintain in the replacement.

For example, this is the content of MyFile.txt:

    payinstr.one.enabled=false
    payinstr.two.enabled=false
    payinstr.three.enabled=false
    [..]

I want to obtain:

payinstr.one.enabled=true
payinstr.two.enabled=true
payinstr.three.enabled=true
[..]

Is there a way using 'sed' command to change these string with a 'true' value considering that there is a dinamic part on it? Somethings like:

sed -i 's/payinstr.*.enabled=false/payinstr.*.enabled=true/' 

Solution

  • Use a capture group with extended regular expressions (-E or -r):

    sed -i -E 's/payinstr\.(.+)\.enabled=false/payinstr.\1.enabled=true/'
    

    or more concisely,

    sed -i -E 's/(payinstr\..+\.enabled=)false/\1true/'
    

    Try it online.