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


In reply to Re: Sort files by date in the file name. by johngg
in thread Sort files by date in the file name. by leoberbert

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.