It seems as if the only blanks that you need to match are the ones between the variable name and the equal sign. Whatever comes after the equal sign will be replaced anyway.
Using standard sed
:
sed 's/^\(MY_VAR\)[[:blank:]]*=.*/\1=NO/' params.txt
The [[:blank:]]*
sub-expression will match zero or more blank characters. A "blank character" is a tab or a space. The substitution replaces the the whole line that starts with the variable's name followed by optional blanks and a =
with the variable's name and the string =NO
.
Testing:
$ cat params.txt
MY_VAR=YES
MY_VAR = MAYBE
MY_VAR = OK
$ sed 's/^\(MY_VAR\)[[:blank:]]*=.*/\1=NO/' params.txt
MY_VAR=NO
MY_VAR=NO
MY_VAR=NO
For in-place editing of the params.txt
file, use -i
correctly depending on what sed
you use (see How can I achieve portability with sed -i (in-place editing)?), or use
cp params.txt params.txt.tmp &&
sed 's/^\(MY_VAR\)[[:blank:]]*=.*/\1=NO/' params.txt.tmp >params.txt &&
rm -f params.txt.tmp