Sed – How to Match Multiple Patterns and Change One Part

sed

I have the below lines in a file:

SUT_INST_PIT=true
SUT_INST_TICS=true
SUT_INST_EXAMPLES=false
SUT_INST_PING=false

How can i create a sed line to match pattern SUT_INST_EXAMPLES & SUT_INST_PING and set false to true?

I can't simply replace false with true because I don't want to change SUT_INST_PIT & SUT_INST_TICS even if they are false!!!

I have at the moment two sed commands that are working, but i would like one line only!

sed -i "s/SUT_INST_EXAMPLES=false/SUT_INST_EXAMPLES=true/g" <file>
sed -i "s/SUT_INST_PING=false/SUT_INST_PING=true/g" <file>

One more thing the sed line should be able to be parametrized to set false->true or true->false, but only for SUT_INST_EXAMPLES & SUT_INST_PING.

Solution (according to @RomanPerekhrest) and how to use it in send (expect script):

send "sed -i 's\/^\\(SUT_INST_EXAMPLES\\|SUT_INST_PING\\)=false\/\\1=true\/' file\r"

Best Answer

sed approach:

sed -i 's/^\(SUT_INST_EXAMPLES\|SUT_INST_PING\)=false/\1=true/' file

file contents:

SUT_INST_PIT=true
SUT_INST_TICS=true
SUT_INST_EXAMPLES=true
SUT_INST_PING=true

\(SUT_INST_EXAMPLES\|SUT_INST_PING\) - alternation group, matches either SUT_INST_EXAMPLES OR SUT_INST_PING at the start of the string


Alternative gawk(GNU awk) approach:

gawk -i inplace -F'=' -v OFS='=' '$1~/^SUT_INST_(EXAMPLES|PING)/{$2=($2=="false")? "true":"false"}1' file
Related Question