Needs perl script - pick right file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Needs perl script - pick right file name
# 1  
Old 03-19-2015
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:

Code:
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

Last edited by rbatte1; 03-19-2015 at 02:53 PM.. Reason: Added CODE & ICODE tags for clarity
# 2  
Old 03-19-2015
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
# 3  
Old 03-19-2015
want to write in perl scripting...not shellscript.
# 4  
Old 03-20-2015
Code:
$
$
$ 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
$
$
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Should pick latest file within past 3 days using UNIX script and perform steps in message below.

Hi , Can anyone help me how do perform below requirement in unix. Step1:we will receive multiple files weekly with same name(as below) in a folder(In folder we will have other files also def.dat,ghf.dat) Filenames: 1) abc_20171204_052389.dat 2)abc_20171204_052428.dat DON'T modify... (23 Replies)
Discussion started by: sunnykamal59
23 Replies

2. Shell Programming and Scripting

Control - M Scheduler is not able to pick my shell script

Hi, Below is a shell script that i made:- #!/bin/ksh #path=/opt/tibco/shared/adaptadores/SSCC/EVEREST/input/ if ; then echo "ZIP Exists and now Processing" for files in /opt/tibco/shared/adaptadores/SSCC/EVEREST/input/T010B04.* do unzip $files echo "Files Unzipped" echo $files... (4 Replies)
Discussion started by: mmtrexon
4 Replies

3. Post Here to Contact Site Administrators and Moderators

Control - M Scheduler is not able to pick my shell script

Hi, Below is a shell script that i made:- #!/bin/ksh #path=/opt/tibco/shared/adaptadores/SSCC/EVEREST/input/ if ; then echo "ZIP Exists and now Processing" for files in /opt/tibco/shared/adaptadores/SSCC/EVEREST/input/T010B04.* do unzip $files echo "Files Unzipped" echo $files... (1 Reply)
Discussion started by: mmtrexon
1 Replies

4. Shell Programming and Scripting

Shell script to pick the oldest file

hi i need shell scrip to pick the oldest file... i.e i need a script to pick the oldest file I had input files in my input folder that are created on saturday,monday,tuesday .. i want a script such that first the script should pick saturday file ,then monday and then file created on... (5 Replies)
Discussion started by: hemanthsaikumar
5 Replies

5. Shell Programming and Scripting

Script pick elements according to the date in file

Hello, i am having file details having elements mentioned below Asia jan 18 Europe Jan 19 America Jan 20 I made script but it picks all the elements, i want to pick the elements according to the date to run the script i made cron job. Please advise how it can work.... (12 Replies)
Discussion started by: rajjev_saini123
12 Replies

6. Shell Programming and Scripting

Pick files after specified Time from a folder in Perl

Hi All I am stuck in a problem and wants help What i want to do is I have a directory(say name of that directory is outdir) In that directory(outdir) the files come after some gap of time by some processes. What i want to do is i want to list all the files in that directory after the given... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

7. UNIX for Dummies Questions & Answers

Script to pick the files from source folder

Hi, I wrote a script to pick files based on some logic. when I execute my script, I get the below message ( I bolded the lines in the script where this error message shows up), it says that files are not available and it is a valid scenario if the files are not available and I don't know how... (9 Replies)
Discussion started by: Vijay81
9 Replies

8. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

9. UNIX for Dummies Questions & Answers

unix script that will pick up first 10 file

Suppose I have a unix file which contain a lost of 60 files like filename1 filename2 ... .. ... filename60 I want to write a unix script that will pick up first 10 files in first run 10-20 files in 2 run 20-30 files in 3 run 30-40 files in 4 run 40-50 files in 5 run 50-60 files in 6... (1 Reply)
Discussion started by: er_zeeshan05
1 Replies

10. Shell Programming and Scripting

Script to pick out files from a file which are older than 15 days

Hi , I have a file abc.txt which actually contains a directory and file list -rwxrwxr-x 1 dmadmin mmasapp 334 Dec 03 2001 aafs_006.ksh -rwxrwxr-x 1 dmadmin mmasapp 1270 Nov 13 2001 mcl_deposit_ftp.ksh -rwxrwxr-x 1 dmadmin mmasapp 925 Oct 31 2001 ... (1 Reply)
Discussion started by: viv1
1 Replies
Login or Register to Ask a Question