perl -wne 's/(.)\1+(?!\1)/length($&)>2?qq[$&]:qq[($1$1)]/ge;print' file.txt