#!/bin/bash
s="#Foo bar
Foo bar
#Baz foo
Baz foo"
perl -pe 's/o{2,}/"O" x length($&)/ge' <<< "$s"