Assume a multi-line text string in which some lines start with a key-character ("#" in our case). Further assume that you wish to replace all instances of a target character ("o" in our case) with a different character ("O" in our case), if - and only if - that target character occurs as a string of two or more adjacent copies (e.g., "ooo"). This replacement is to be done in all lines that do not start with the key-character and must be case-sensitive.
For example, the following lines ...
#Foo bar
Foo bar
#Baz foo
Baz foo
are supposed to be converted into:
#Foo bar
FOO bar
#Baz foo
Baz fOO
The following attempt using sed does not retain the correct number of target characters:
$ echo -e "#Foo bar\nFoo bar\n#Baz foo\nBaz foo" | sed '/^#/!s/o\{2,\}/O/g'
#Foo bar
FO bar
#Baz foo
Baz fO
What code (with sed or otherwise) would conduct the desired replacement correctly?