This gives your "expected output" but there's a conflict between my sort and your sort description.

#!/usr/bin/perl -l # http://perlmonks.org/?node_id=1198131 use strict; use warnings; my %one; while(<DATA>) { my ($one, $two, $three, $four) = split; push @{ $one{$one} }, [ $two, $three, $four ]; } for my $one (sort 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 ) = @$_; if( keys %groups ) { for ( sort keys %groups ) { if( ( $_ ^ "$three" ) =~ tr/\0//c <= 2 ) { push @{ $groups{$_} }, [ $two, $three, $four ]; next LOOP; } } push @{ $groups{$three} }, [ $two, $three, $four ]; } else { push @{ $groups{$three} }, [ $two, $three, $four ]; } } for ( sort keys %groups ) { print 'CLUSTER'; for ( values @{ $groups{$_} } ) { print join "\t", $one, @$_; } } } __DATA__ chrM:307 0 AGCGGGGA 129 chrM:307 0 AGCGGGGA 130 chrM:307 0 AGCGGGGA 129 chrM:308 0 AGCGGGGA 129 chrM:308 0 AGCGGGGA 130 chrM:308 0 AGCGGGGA 129 chrM:309 0 AGCGGGGA 129 chrM:309 0 AGCGGGGA 130 chrM:309 0 AGCGGGGA 129 chrM:307 0 TCAAAATG 130 chrM:308 0 TCAAAATG 130 chrM:309 0 TCAAAATG 130 chrM:307 0 TCACGGTG 130 chrM:308 0 TCACGGTG 130 chrM:309 0 TCACGGTG 130 chrM:307 0 TCAGCCTG 129 chrM:308 0 TCAGCCTG 129 chrM:309 0 TCAGCCTG 129 chrM:307 0 TCAGGGAG 130 chrM:308 0 TCAGGGAG 130 chrM:309 0 TCAGGGAG 130 chrM:307 1 TCAGGGTG 106 chrM:307 2 TCAGGGTG 130 chrM:307 2 TCAGGGTG 129 chrM:308 1 TCAGGGTG 106 chrM:308 2 TCAGGGTG 130 chrM:308 2 TCAGGGTG 129 chrM:309 1 TCAGGGTG 106 chrM:309 2 TCAGGGTG 130 chrM:309 2 TCAGGGTG 129

Output:

CLUSTER chrM:307 0 AGCGGGGA 130 chrM:307 0 AGCGGGGA 129 chrM:307 0 AGCGGGGA 129 CLUSTER chrM:307 0 TCAAAATG 130 CLUSTER chrM:307 0 TCACGGTG 130 chrM:307 0 TCAGGGAG 130 chrM:307 2 TCAGGGTG 130 chrM:307 2 TCAGGGTG 129 chrM:307 1 TCAGGGTG 106 CLUSTER chrM:307 0 TCAGCCTG 129 CLUSTER chrM:308 0 AGCGGGGA 130 chrM:308 0 AGCGGGGA 129 chrM:308 0 AGCGGGGA 129 CLUSTER chrM:308 0 TCAAAATG 130 CLUSTER chrM:308 0 TCACGGTG 130 chrM:308 0 TCAGGGAG 130 chrM:308 2 TCAGGGTG 130 chrM:308 2 TCAGGGTG 129 chrM:308 1 TCAGGGTG 106 CLUSTER chrM:308 0 TCAGCCTG 129 CLUSTER chrM:309 0 AGCGGGGA 130 chrM:309 0 AGCGGGGA 129 chrM:309 0 AGCGGGGA 129 CLUSTER chrM:309 0 TCAAAATG 130 CLUSTER chrM:309 0 TCACGGTG 130 chrM:309 0 TCAGGGAG 130 chrM:309 2 TCAGGGTG 130 chrM:309 2 TCAGGGTG 129 chrM:309 1 TCAGGGTG 106 CLUSTER chrM:309 0 TCAGCCTG 129

In reply to Re: 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.