perl -e '$gh = join qq[],(); if ($gh =~ m@(\b(\*|_)\S+\b)(.+?)(\b\S+\2\b)@s) {print join q[ ],$1,$3,$4,q[ ];}' Happy _puppy life good_ yeah.