Picking the latest file based on a timestamp for a Dynamic file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Picking the latest file based on a timestamp for a Dynamic file name
# 1  
Old 06-20-2016
Picking the latest file based on a timestamp for a Dynamic file name

Hi ,

I did the initial search but could not find what I was expecting for.

Code:
15606Always_9999999997_20160418.xml
15606Always_9999999998_20160418.xml
15606Always_9999999999_20160418.xml
9819Always_99999999900_20160418.xml
9819Always_99999999911_20160418.xml

Above is the list of files I receive.

The file name is started with a key value and its changes.

I need to pick the latest file based on the seconds (_99999999911_) in the file name for each key value.

The result would be

Code:
15606Always_9999999999_20160418.xml
9819Always_99999999911_20160418.xml

I have script to select if the file name pattern does not change .. But in my case the key value is present in the starting of the file name.

Any help would be appreciated.


Thank you
# 2  
Old 06-20-2016
Like so:
Code:
for FN in $(ls -r *.xml); do KEY=${FN%%[A-Z]*}; [ "$KEY" = "$OLDKEY" ] || echo $FN; OLDKEY=$KEY; done
9819Always_99999999911_20160418.xml
15606Always_9999999999_20160418.xml

?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-20-2016
Thanks a lot .. This loop worked after I initialized the OLDKEY variable.

But I am still not clear how the code is working and getting the result. I believe I understood the part where you are storing the key value in to $KEY on a -r result .. But not clear how the latest file is picked based on the seconds in the file.

It will be of great help if you can explain how the code is working.

Thank you
# 4  
Old 06-20-2016
The files are sorted in reverse order; that why the "command substitution" for ls needs to be used. So per key, the latest version is the first one. For a new key, the first line is printed as OLDKEY still holds the old key, and key is stored in OLDKEY for further reference.
# 5  
Old 06-20-2016
With awk it looks simpler:
Code:
ls -r | awk -F_ '/\.xml$/ && $1!=p1 {print; p1=$1}'

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help with UNIX command to get the latest file from list of files with timestamp

Hi All, I have list of files like below with name abcxyz.timestamp. I need a unix command to pick the latest file from the list of below files. Here in this case the lates file is abcxyz.20190304103200. I have used this unix command "ls abcxyz*|tail -1" but i heard that it is not the appropriate... (2 Replies)
Discussion started by: rakeshp
2 Replies

2. Shell Programming and Scripting

Search for latest Timestamp in file

Hi, I have a file which generate Timestamp in this format :- 20121012162354 20121114191610 20121210232808 20121216220002 20130106220002 20130127220001 20130203220001 20121012162354 20121114191610 20121210232808 20121216220002 20130106220002 20130127220001 20130203220001 (2 Replies)
Discussion started by: netdbaind
2 Replies

3. UNIX for Dummies Questions & Answers

Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (12 Replies)
Discussion started by: robertbrown624
12 Replies

4. Shell Programming and Scripting

Urgent ...pls Sorting files based on timestamp and picking the latest file

Hi Friends, Newbie to shell scripting. Currently i have used the below to sort data based on filenames and datestamp $ printf '%s\n' *.dat* | sort -t. -k3,4 filename_1.dat.20120430.Z filename_2.dat.20120430.Z filename_3.dat.20120430.Z filename_1.dat.20120501.Z filename_2.dat.20120501.Z... (1 Reply)
Discussion started by: robertbrown624
1 Replies

5. Shell Programming and Scripting

Picking contents value of file at run time based on variable values

HI, I have to write a unix script and need your help. in my application where I have to invoke this script a varialble is there where the value comes in a variable . for example variable can be var=ABC like this there will be any type of value in the vcariable. there is a unix directory... (2 Replies)
Discussion started by: manish8484
2 Replies

6. UNIX for Dummies Questions & Answers

How to pick only the latest files based on the timestamp?

I have a few log files which get generated on a daily basis..So, I need to pick only the ones which get generated for that particular day. -rw-r--r-- 1 staff 510732676 Apr 7 22:01 test.log040711 -rwxrwxrwx 1 staff 2147482545 Apr 7 21:30 test.log.2 -rwxrwxrwx 1 staff 2147482581 Apr 7 19:26... (43 Replies)
Discussion started by: win4luv
43 Replies

7. Shell Programming and Scripting

grep latest file based on date.

hi all, not sure if this has been posted b4 but i try to search but not valid. this is my question: when i do a ls -ltr there will be a list generated as follows: -rw-r--r-- 1 root sys 923260 Jan 10 04:38 FilePolling.41025.083TL021.xml -rw-r--r-- 1 root sys 1761337 Jan 10 04:40... (12 Replies)
Discussion started by: lweegp
12 Replies

8. AIX

Unix shell scripting to find latest file having timestamp embedded...

Hi guys, I have a directory in UNIX having files with the below format, i need to pickup the latest file having recent timestamp embedded on it, then need to rename it to a standard file name. Below is the file format: filename_yyyymmdd.csv, i need to pick the latest and move it with the... (2 Replies)
Discussion started by: kaushik25
2 Replies

9. Shell Programming and Scripting

finding latest file having timestamp on it.....

Hi guys, I have a directory in UNIX having files with the below format, i need to pickup the latest file having recent timestamp embedded on it, then need to rename it to a standard file name. Below is the file format: filename_yyyymmdd.csv, i need to pick the latest and move it with the... (1 Reply)
Discussion started by: kaushik25
1 Replies

10. Shell Programming and Scripting

Picking the file based on Date..Requirement

Dear frnds My requirement is as follows -rw-r----- 1 f02 dd 109428250 May 18 14:02 Extracts_20070518104730.zip -rw-r----- 1 f02 dd 109493187 May 21 13:30 Extracts_20070521091700.zip -rw-r----- 1 f02 dd 109993058 May 23 14:14 Extracts_20070523085955.zip -rw-r----- 1... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies
Login or Register to Ask a Question