#!perl use strict; # First, group them by the first part of the numeric field # and the other two columns # then find how much of the rest of the numeric field is needed # to distinguish the variations in the third column my %grouping; while() { my ($n, $n2, $c2, $c3) = /(\d+)\.(\S+)\s+(\w+)\s+(\w+)/; $grouping{$n}{$c2}{$c3}{$n2} = $_; } for my $n (sort {$a <=> $b} keys %grouping) { my $N = $grouping{$n}; for my $c2 (sort keys %$N) { my $C2 = $N->{$c2}; if (keys %$C2 > 1) { # print "$n$c2 is a complex group, containing:\n"; for my $c3 (sort keys %$C2) { my $C3 = $C2->{$c3}; # print "..(for c3=$c3):\n"; for my $n (sort keys %$C3) { print "$C3->{$n}"; } } } else { print "$n$c2 contains:\n"; my $C3 = $C2->{(keys %$C2)[0]}; for my $n (sort keys %$C3) { print " $C3->{$n}"; } } } } __DATA__ 0024.118.3A FT TFF 0024.118.3B FT TFF 0024.118.3C FT TFF 0024.118.3D FT TFF 0024.118.4 FF TFF 0039.04 NN OTH 0039.205.2 FT OTH 0039.205.6 FT TFF 409.176.12A FT TFF 409.176.12B FT TFF 409.176.12C FT TFF 409.176.12D FT OTH 409.176.12E FT OTH