in reply to Sort files by date in the file name.

The following code finds the oldest file for each "first column" set, if there are multiple oldest files it will display only the first in lexical order. It is not clear whether the date format is YYYYMMDD as in the OP or DDMMYYYY as per the later posted data; I have gone with the latter.

johngg@shiraz:~/perl/Monks > perl -Mstrict -Mwarnings -E ' my @files = qw{ 981139804_ABCDEF12345678765463565E_28062016172508.TXT 981139804_ABCDEF12345678765463565E_28062016192508.TXT 981139804_ABCDEF12345678765463565E_28062016112508.TXT 981139804_ABCDEF12345678765463565E_28062016102508.TXT 981139804_ABCDEF12345678765463565E_28062017102508.TXT 971305332_XXXXXX12345678765463565E_28062011102508.TXT 971305332_AAAAAAAA12345678765463565E_28062011102508.TXT 971305332_CCCC12345678765463565E_28062011102508.TXT 971305332_CCCC12345678765463565E_28062020102508.TXT }; my @oldest = do { my %seen; grep { not $seen{ ( split m{_} )[ 0 ] } ++ } map { substr $_, 21 } sort map { my( $col1, $dt ) = ( split m{[_.]} )[ 0, 2 ]; my( $d, $m, $y, $t ) = unpack q{a2 a2 a4 a6}, $dt; pack q{a9 a4 a2 a2 a6 a*}, $col1, $y, $m, $d, $t, $_; } @files; }; say for @oldest;' 08971305332_AAAAAAAA12345678765463565E_28062011102508.TXT 08981139804_ABCDEF12345678765463565E_28062016102508.TXT

I hope this guess at your intent is correct.

Cheers,

JohnGG