Like this? (Just a few minor tweaks :)

BTW, it was sorting, just not numerically.

#!/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 chrM:1257 1 TGGAAATC 129 chrM:1262 0 CGGGAAGC 129 chrM:1262 0 AGGGAAGG 129 chrM:1603 0 GTATCGGA 130 chrM:1603 1 GTATCGGA 130

Outputs:

CLUSTER chrM:307 0 TCAGGGTG 130 chrM:307 0 TCAGGGTG 115 chrM:307 0 TCAGGGTG 115 chrM:307 0 TCAGGGTG 114 chrM:307 0 TCAGGGTG 107 chrM:307 1 TCAGGGTG 106 CLUSTER chrM:310 0 TCAGGGTG 99 CLUSTER chrM:392 2 CCTCTTAT 130 CLUSTER chrM:396 2 AGTTACTA 129 CLUSTER chrM:443 0 ATTATCAA 130 extra columns CLUSTER chrM:542 0 AATCCAAA 129 chrM:542 2 AATCCAAA 129 CLUSTER chrM:934 0 CATTCGCT 129 chrM:934 1 CATTCGCT 129 CLUSTER chrM:1001 0 CGTGACAT 129 CLUSTER chrM:1127 0 GATACTAA 130 CLUSTER chrM:1257 1 TGGAAATC 129 CLUSTER chrM:1262 0 AGGGAAGG 129 chrM:1262 0 CGGGAAGC 129 CLUSTER chrM:1603 0 GTATCGGA 130 chrM:1603 1 GTATCGGA 130

In reply to Re^3: Find duplicate based on specific fields while allowing 2 mismatch by tybalt89
in thread Find duplicate based on specific fields while allowing 2 mismatch by amitgsir

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.