G'day leoberbert,
I think this does what you want:
#!/usr/bin/env perl -l use strict; use warnings; my @files = qw{ 1020300000_XXXXXXXXX_20160707193000.TXT 1020300000_XXXXXXXXX_20160707170000.TXT 1020400000_XXXXXXXXX_20160707180000.TXT 1020400000_XXXXXXXXX_20160707190000.TXT }; my @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] || $a->[3] <=> $b->[3] } map { [ $_ => split /[_.]/ ] } @files; print join "\n", '@files:', @files; print join "\n", '@sorted:', @sorted;
Output:
@files: 1020300000_XXXXXXXXX_20160707193000.TXT 1020300000_XXXXXXXXX_20160707170000.TXT 1020400000_XXXXXXXXX_20160707180000.TXT 1020400000_XXXXXXXXX_20160707190000.TXT @sorted: 1020300000_XXXXXXXXX_20160707170000.TXT 1020300000_XXXXXXXXX_20160707193000.TXT 1020400000_XXXXXXXXX_20160707180000.TXT 1020400000_XXXXXXXXX_20160707190000.TXT
The construct I've used here is known as a Schwartzian Transform.
This sorts first on column1 and then on column3, both in ascending order; this should put your wanted file in $sorted[0]. If that's not what you want, modify the sort { ... } code to suit.
— Ken
In reply to Re: Sort files by date in the file name.
by kcott
in thread Sort files by date in the file name.
by leoberbert
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |