get the latest file by reading the date in the filename.


 
Thread Tools Search this Thread
# 1  
get the latest file by reading the date in the filename.

Hi,
I grep for a pattern in a list of files.

"grep -i -l $pattern *.datx*"

it may give me n number of files.
say for eg, it gives me 2 files.
lock_eicu_20071228_00000000.dat_20071228_05343100
lock_eicu_20080501_00000000.dat_20080501_05343900

out of these 2 files I need to get the latest file according to the date present after the .dat extn. As 20071228 is older than 20080501, i need to get the second file.

so in the above eg, i need to get the filename
lock_eicu_20080501_00000000.dat_20080501_05343900
can u please help.
# 2  
Assuming all the names are in the same format:

Code:
awk -F_ 'm<$5{m=$5;f=$0}END{print f}'

# 3  
Many Thanks Frank, it works perfect.
# 4  
Hi frank, one more help please,
I do below to list my files in chronological order.
ls -ltr *.datx* | tr -s ' ' | cut -d' ' -f9

it gives me list of files like below.

lock_eicu_20081120_00000000.datx_20081120_05315400
lock_eicu_20081118_00000000.datx_20081118_06303100
lock_eicu_20081111_00000001.datx_20081111_15531300
lock_eicu_20081111_00000000.datx_20081111_06335300
lock_eicu_20081030_00000000.datx_20081030_05342900
lock_eicu_20081016_00001111.datx_20081016_15525600
lock_eicu_20081016_00000000.datx_20081016_06350500
lock_eicu_20081001_00000000.datx_20081001_05344100
lock_eicu_20080912_00000000.datx_20080912_05305200
lock_eicu_20080904_00000000.datx_20080904_05335500
lock_eicu_20080822_00000000.datx_20080822_05344200
lock_eicu_20080812_00000000.datx_20080812_06331200
lock_eicu_20080808_00000000.datx_20080808_05345200
lock_eicu_20080806_00000000.datx_20080806_06343800
lock_eicu_20080802_00000000.datx_20080804_05332000
lock_eicu_20080718_00000000.datx_20080718_06533100
lock_eicu_20080606_00000000.datx_20080606_05340600
lock_eicu_20080603_00000000.datx_20080603_06345300
lock_eicu_20080531_00000000.datx_20080602_05304300
lock_eicu_20080529_00000000.datx_20080529_06330500
lock_eicu_20080501_00000000.datx_20080501_05343900
lock_eicu_20080429_00000000.datx_20080429_06311200
lock_eicu_20071228_00000000.datx_20071228_05343100

how do i sort the files again chronologically as per the date found just after .datx?
i mean in the list if u see "lock_eicu_20071228_00000000.datx_20071228_05343100" is the oldest file as it has date 20071228 after datx. so i need to list this file first and then sort so on.

Please help. Thanks again
# 5  
Try:

Code:
sort -n -t_ -k5

# 6  
Thanks a lot Frank. it works perfect.
# 7  
hey Franklin,

care to explain the breakdown of the awk statement?

thanks
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #605
Difficulty: Medium
As of MySQL 8.0.14, the ADD DATAFILE clause of the CREATE TABLESPACE statement is mandatory.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read latest file name with a date and time and then download from FTP

Hi All, Please help. I have requirement to read the file / folder based on the latest date and download the file and folder. There will be files and folders in the location like 20140630-144422 20140630-144422.csv 20140707-182653 20140707-182653.csv 20140710-183153... (7 Replies)
Discussion started by: Praveen Pandit
7 Replies

2. UNIX for Dummies Questions & Answers

How to pick the latest file with date as one among the file name.( not exactly present date.)?

i have files like 1)20131112_abc_01.csv and 2)20131113_abc_01.csv and 3)20131113_abc_02.csv when i try to fetch the file in the next day. it shud pick the third file.. plz help me.. and i use `date +"%Y%m%d"` command to fetch..it fetches the current date... (2 Replies)
Discussion started by: applepie
2 Replies

3. Shell Programming and Scripting

Find the latest file based on the date in the filename

Hi, We've a list of files that gets created on a weekly basis and it has got a date and time embedded to it. Below are the examples. I want to find out how to get the latest files get the date and time stamp out of it. Files are PQR123.PLL.M989898.201308012254.gpg... (1 Reply)
Discussion started by: rudoraj
1 Replies

4. Shell Programming and Scripting

How to extract latest file by looking at date time stamp from a directory?

hi, i have a Archive directory in which files are archived or stored with date and time stamp to prevent over writing. example: there are 5 files s1.txt s2.txt s3.txt s4.txt s5.txt while moving these files to archive directory, date and time stamp is added. of format `date... (9 Replies)
Discussion started by: Little
9 Replies

5. Shell Programming and Scripting

Needed shell script to get the latest file based on date

hi i need the shell script to get the file with latest date. for example in my input folder i have files like file_20130212.txt file_20130211.txt now my output folder should have the file with latest date i.e..file_20120212.txt i want to get the latest file .. i.e is should take... (6 Replies)
Discussion started by: hemanthsaikumar
6 Replies

6. UNIX for Dummies Questions & Answers

Selecting the file of latest Date

Hi Folks, I have one query that there is a folder in which daily several logs files are getting created , I reached to that location through putty but what I observer that 10 files of different date are been created with same name , what I need to see is the latest file ...let say the location is ... (5 Replies)
Discussion started by: KAREENA18
5 Replies

7. Shell Programming and Scripting

Grep the Content of a LOG File which has latest Date and Time

Hi All, Need a small help. I have a log file which keeps updating for every Minute with multiple number of lines. I just want to grep few properties which has latest Date and Time to it. How do i do it? I wanted to grep a property by name "Reloading cache with a maximum of" from the... (4 Replies)
Discussion started by: nvindraneel
4 Replies

8. Homework & Coursework Questions

Matlab help! Reading in a file with a variable filename

1. The problem statement, all variables and given/known data: I want to read in a file, and plot the data in matlab. However, I do not like hardwiring filenames into my codes, so I always give the user the option to specify what the filename is. I am pretty inexperienced with matlab, so I have no... (0 Replies)
Discussion started by: ds7202
0 Replies

9. 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

10. Shell Programming and Scripting

Reading a file using sh with spaces in filename

Hi I am trouble parsing through a file with spaces in the filename. I need to grab "supportIDPS/SCM/windows_install/file groups/dds.fgl" and then do a md5sum on it. I am using sh. Any help is appreciated. Here is an example of the input file: 7eedbc9f7902bf4c1878d9e571addf9a ... (4 Replies)
Discussion started by: jekl5
4 Replies

Featured Tech Videos