perl -le 'print $x = "abcdefgxxabcdefgzzabcdsjfhkdfab"; print $2 if $x =~ /((\w{2,}).*?\2.*?)+/;' abcdefgxxabcdefgzzabcdsjfhkdfab abcdefg