Next time be explicit that you wanted it sorted by the number AFTER the colon.
#!/usr/bin/perl
# http://perlmonks.org/?node_id=1198131
use strict;
use warnings;
my %one;
while(<DATA>)
{
my ($one, $two, $three, $four) = split;
/:(\d+)/; # get number for later sort
push @{ $one{$1} }, [ $two, $three, $four, $_ ];
}
for my $one (sort { $a <=> $b } keys %one)
{
my %groups;
LOOP:
for ( sort {
$a->[1] cmp $b->[1] or
$b->[2] <=> $a->[2] or
$a->[0] <=> $b->[0] }
@{ $one{$one} } )
{
my ( $two, $three, $four, $all ) = @$_;
if( keys %groups )
{
for ( sort keys %groups )
{
if( ( $_ ^ "$three" ) =~ tr/\0//c <= 2 )
{
push @{ $groups{$_} }, [ $two, $three, $four, $all ];
next LOOP;
}
}
push @{ $groups{$three} }, [ $two, $three, $four, $all ];
}
else
{
push @{ $groups{$three} }, [ $two, $three, $four, $all ];
}
}
for ( sort keys %groups )
{
print "CLUSTER\n";
print $_->[3] for values @{ $groups{$_} };
}
}
__DATA__
chrM:307 0 TCAGGGTG 115
chrM:307 0 TCAGGGTG 107
chrM:307 0 TCAGGGTG 115
chrM:307 0 TCAGGGTG 130
chrM:307 0 TCAGGGTG 114
chrM:307 1 TCAGGGTG 106
chrM:310 0 TCAGGGTG 99
chrM:392 2 CCTCTTAT 130
chrM:396 2 AGTTACTA 129
chrM:443 0 ATTATCAA 130 extra columns
chrM:542 2 AATCCAAA 129
chrM:542 0 AATCCAAA 129
chrM:934 0 CATTCGCT 129
chrM:934 1 CATTCGCT 129
chrM:1001 0 CGTGACAT 129
chrM:1127 0 GATACTAA 130
chr1:1257 1 TGGAAATC 129
chr1:1262 0 CGGGAAGC 129
chr1:1262 0 AGGGAAGG 129
chr1:1603 0 GTATCGGA 130
chr1:1603 1 GTATCGGA 130