{
i=0
while IFS= read -r line; do
case "$line" in
ssh*|'##'*)
;;
SERVER*)
((++i))
;;
*)
if ((i>0)); then echo $i;i=0; fi
echo "$line"
;;
esac
done
if ((i>0)); then echo $i;i=0; fi
} <inputfile >outputfile
O mesmo em perl one-liner
perl -nle '
BEGIN{$i=0}
next if/^(ssh|##)/;
if(/^SERVER/){++$i;next}
print$i if$i>0;
$i=0;
print;
END{print$i if$i>0}' inputfile >outputfile
e golfe
perl -nle's/^(ssh|##|(SERVER))/$2&&$i++/e&&next;$i&&print$i;$i=!print}{$i&&print$i' inputfile >outputfile