This solution implements the approach to split the line at PATTERN (split), substitute the first false in the second part (sub) and combine the parts (for loop and printf). The next command skips further processing of this input line. Other lines are printed unchanged. (1 is an always true condition with default action.)
awk '/PATTERN.*false/ {
n=split($0,parts,"PATTERN");
sub("false", "true", parts[2]);
for(i=1;i<n;i++) {
printf("%s%s", parts[i], "PATTERN");
}
printf("%s\n", parts[n]);
next }
1'
It is unclear from the question if the value that corresponds to PATTERN is always false, so it may replace a wrong false.
Sample input
colorA is false colorB is false PATTERN is false colorC is false colorD is false
colorA is false colorB is false PATTERN is true colorC is false colorD is false
results in this output
colorA is false colorB is false PATTERN is true colorC is false colorD is false
colorA is false colorB is false PATTERN is true colorC is true colorD is false
Edit according to RudiC's comment: If the to-be-modified value after PATTERN is either "true" or "false", then this possible problem can be avoided by replacing the instruction sub("false", "true", parts[2]); with sub("false|true", "true", parts[2]);
awk '/PATTERN.*false/ {
n=split($0,parts,"PATTERN");
sub("false|true", "true", parts[2]);
for(i=1;i<n;i++) {
printf("%s%s", parts[i], "PATTERN");
}
printf("%s\n", parts[n]);
next }
1'
With the same sample input this results in
colorA is false colorB is false PATTERN is true colorC is false colorD is false
colorA is false colorB is false PATTERN is true colorC is false colorD is false
PATTERNalways be without a whitespace? AfterPATTERN, will there always be " is true" (exactly), when you have to change it to " is false"?PATTERNWill have no whitespace. BetweenPATTERNandtruehave unknown number of words.awkregular expressions are greedy. That means a RegExpPATTERN.*falsewill match everything up to the very lastfalseon the line. You may have to resort to aperl-based solution where greedyness can be turned off.colorX. Can we rely on a fixed pattern for the next word after thefalseyou want to turn intotrue?PATTERN, substitute the firstfalsein the second part and combine the parts. Are you sure that the firstfalseafterPATTERNbelongs toPATTERN? Or in other words: Are you sure that the value that belongs toPATTERNis alwaysfalseand nottrue? What about a linecolorA is false colorB is false PATTERN is true colorC is false colorD is false?