in reply to Re^3: Sort files by date in the file name.
in thread Sort files by date in the file name.

See this example: 1020300000_XXXXXXXXX_20160707193000.TXT 1020300000_XXXXXXXXX_20160707170000.TXT I need to return only the oldest file using the file name date. 1020300000_XXXXXXXXX_20160707170000.TXT
  • Comment on Re^4: Sort files by date in the file name.

Replies are listed 'Best First'.
Re^5: Sort files by date in the file name.
by Anonymous Monk on Jul 08, 2016 at 00:07 UTC
    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; my @in = qw/ 1020300000_XXXXXXXXX_20160707193000.TXT 1020300000_XXXXXXXXX_20160707170000.TXT 1020300000_XXXXXXXXX_20160707190000.TXT /; my $final = GiveMeItTheThingIWant( \@in ); dd( $final, \@in ); sub GiveMeItTheThingIWant { my( $in ) = @_; my @ordered = map { $$_[1] } sort { $$a[0] cmp $$b[0] } map { #~ my( $left, $right ) = $_ =~ m{^([^_]+)_[^_]+_([^_]+)}; my( $left, $middle, $right , $txt ) = split /[_\.]/, $_; [ $right, $_ ]; } @{$in}; return $ordered[0]; #~ return '1020300000_XXXXXXXXX_20160707170000.TXT'; } __END__
      Hello friend, thanks for the tip. It has helped me .. but I own many other files as for example: 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 And when that occurs, it is only returned to a number, that is, I needed to return ordering the first column and the last.

        Your OP data has the date in YYYYMMDD format which is simple to sort. The data here is in DDMMYYYY format which is trickier. Which is correct and, more importantly, is the data consistent?

        Cheers,

        JohnGG

        Thats funny leoberbert
Re^5: Sort files by date in the file name.
by Anonymous Monk on Jul 08, 2016 at 00:00 UTC

    Ok thats easier to deduce from, its easier to understand the question if you take one file name, break it into parts, and label the parts so they match the word descriptions ... I'll be back in 1min