I this what you needed? I'm not sure?
Update: I saw the post from johngg at Re^7: Sort files by date in the file name.. I had completely missed the fact that the date format is different in this second data set. If this is the format, then a simple numeric date comparison won't work and something more involved is required. However, when I see at date like 28062017102508,
in 2017, that makes me think that this data was hand generated. Note to OP when you post example data, make it as close to the "real thing" as possible. These details matter.
Also, the sort method I show below is basic. Although straight forward, it has a built in inefficiency in that the the splits are run each time a pair of data in $a,$b is selected by the sort algorithm. The Schwartzian Transform as shown by kcott avoids this by calculating the split for all the data at one time. So it is faster, but at the cost of more complexity. I recommend to a beginner to master the basics first, then get fancy once you have a solid foundation.
#!/usr/bin/perl
use strict;
use warnings;
my @files;
while (<DATA>)
{
chomp;
push @files, $_;
}
@files = sort by_first_third @files;
sub by_first_third
{
my ($Afirst,$Athird) = (split '_|\.',$a)[0,2];
my ($Bfirst,$Bthird) = (split '_|\.',$b)[0,2];
$Afirst <=> $Bfirst
or
$Athird <=> $Bthird
}
print join ("\n",@files), "\n\n";
print "first file: $files[0]", "\n";
print "last file : $files[-1]", "\n";
=prints
971305332_XXXXXX12345678765463565E_28062011102508.TXT
971305332_AAAAAAAA12345678765463565E_28062011102508.TXT
971305332_CCCC12345678765463565E_28062011102508.TXT
971305332_CCCC12345678765463565E_28062020102508.TXT
981139804_ABCDEF12345678765463565E_28062016102508.TXT
981139804_ABCDEF12345678765463565E_28062016112508.TXT
981139804_ABCDEF12345678765463565E_28062016172508.TXT
981139804_ABCDEF12345678765463565E_28062017102508.TXT
first file: 971305332_XXXXXX12345678765463565E_28062011102508.TXT
last file : 981139804_ABCDEF12345678765463565E_28062017102508.TXT
=cut
__DATA__
981139804_ABCDEF12345678765463565E_28062016172508.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
|