how to match multiple patterns and change one part with sed

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

You can simply toggle with:

sed -i -E '/^SUT_INST_(PING|EXAMPLES)=/{s/false/true/;t;s/true/false/;}' infile

This will change true to false or false to true depending on the current value.

Tags:

Sed