Needs perl script - pick right file name

Hi.,

I need in perl type ...where this is an example :

assume a directory has 3 files namely:

file1_1445566_201501022.txt
file1_1445566_201502024.txt
file1_1445566_201503009.txt

where ,after second underscore, the number is in YYYYMMDD time-stamp format, I need perl script to pick up the file if the file name with time-stamp is latest, especially pick only - file1_1445566_201503009.txt

Thanks in advance

You have naturally age sorted files by using a good file name standard.

Do you need to feed this into perl from a shell script or actually get perl to choose.

What have you tried so far?

Regards,
Robin

want to write in perl scripting...not shellscript.

$
$
$ ls -1 *.txt
file1_1445566_20140905.txt
file1_1445566_20140916.txt
file1_1445566_20140924.txt
file1_1445566_20141120.txt
file1_1445566_20141127.txt
file1_1445566_20141130.txt
file1_1445566_20141201.txt
file1_1445566_20150122.txt
file1_1445566_20150210.txt
file1_1445566_20150311.txt
$
$ perl -le 'foreach $file (glob "./*.txt") {
                ($ts = $file) =~ s/.*_(\d+)\..*$/$1/;
                $latest_file = $file if $ts > $prev_ts;
                $prev_ts = $ts;
            }
            END { print $latest_file }
           '
./file1_1445566_20150311.txt
$
$
$