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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Urgent ...pls Sorting files based on timestamp and picking the latest file
# 1  
Old 05-17-2012
Data 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
Code:
$ 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
filename_3.dat.20120501.Z
filename_1.dat.20120502.Z
filename_2.dat.20120502.Z
filename_3.dat.20120502.Z
filename_1.dat.20120627.Z
filename_2.dat.20120627.Z
filename_3.dat.20120627.Z
filename_1.dat.20120730.Z
filename_2.dat.20120730.Z
filename_3.dat.20120730.Z

Now i want to delete files with older date and keep 4 latest files available in the filename(no system timestamp here) So my result should be like
Code:
filename_1.dat.20120501.Z
filename_2.dat.20120501.Z
filename_3.dat.20120501.Z
filename_1.dat.20120502.Z
filename_2.dat.20120502.Z
filename_3.dat.20120502.Z
filename_1.dat.20120627.Z
filename_2.dat.20120627.Z
filename_3.dat.20120627.Z
filename_1.dat.20120730.Z
filename_2.dat.20120730.Z
filename_3.dat.20120730.Z


Last edited by fpmurphy; 05-18-2012 at 05:49 AM..
# 2  
Old 05-17-2012
Everyone at the UNIX and Linux Forums gives their best effort to reply to all questions in a timely manner. For this reason, posting questions with subjects like "Urgent!" or "Emergency" and demanding a fast reply are not permitted in the regular forums.

For members who want a higher visibility to their questions, we suggest you post in the Emergency UNIX and Linux Support Forum. This forum is given a higher priority than our regular forums.

Posting a new question in the Emergency UNIX and Linux Support Forum requires forum Bits. We monitor this forum to help people with emergencies, but we do not not guarantee response time or best answers. However, we will treat your post with a higher priority and give our best efforts to help you.

If you have posted a question in the regular forum with a subject "Urgent" "Emergency" or similar idea, we will, more-than-likely, close your thread and post this reply, redirecting you to the proper forum.

Of course, you can always post a descriptive subject text, remove words like "Urgent" etc. (from your subject and post) and post in the regular forums at any time.


Thank you.

The UNIX and Linux Forums
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

List the files after sorting based on file content

Hi, I have two pipe separated files as below: head -3 file1.txt "HD"|"Nov 11 2016 4:08AM"|"0000000018" "DT"|"240350264"|"56432" "DT"|"240350264"|"56432" head -3 file2.txt "HD"|"Nov 15 2016 2:18AM"|"0000000019" "DT"|"240350264"|"56432" "DT"|"240350264"|"56432" I want to list the... (6 Replies)
Discussion started by: Prasannag87
6 Replies

3. Shell Programming and Scripting

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. 15606Always_9999999997_20160418.xml 15606Always_9999999998_20160418.xml 15606Always_9999999999_20160418.xml 9819Always_99999999900_20160418.xml 9819Always_99999999911_20160418.xmlAbove is the list of files I... (4 Replies)
Discussion started by: chillblue
4 Replies

4. Shell Programming and Scripting

I have this list of files . Now I will have to pick the latest file based on some condition

3679 Jul 21 23:59 belk_rpo_error_**po9324892**_07212014.log 0 Jul 22 23:59 belk_rpo_error_**po9324892**_07222014.log 3679 Jul 23 23:59 belk_rpo_error_**po9324892**_07232014.log 22 Jul 22 06:30 belk_rpo_error_**po9324267**_07012014.log 0 Jul 20 05:50... (5 Replies)
Discussion started by: LoneRanger
5 Replies

5. UNIX for Dummies Questions & Answers

Display files based on particular file timestamp

Hi, I have requirement to list out files that are created after particular file. ex. I have below files in my directory. I want to display files created after /dirdat/CG1/cg004440 file. ./dirdat/CG1/cg004438 09/07/14 0:44:05 ./dirdat/CG1/cg004439 09/07/14 6:01:48 ... (3 Replies)
Discussion started by: tmalik79
3 Replies

6. Shell Programming and Scripting

Help me pls : splitting single file in unix into different files based on data

I have a file in unix with sample data as follows : -------------------------------------------------------------- -------------------------------------------------------------- {30001002|XXparameter|Layout|$ I want this file to be splitted into different files and corresponding to the sample... (54 Replies)
Discussion started by: Ravindra Swan
54 Replies

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

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

9. Shell Programming and Scripting

Purge files based on timestamp avl in file name

Dear All, I have the followoing requirement.. REQ-1: Suppose I have the following files XX_20070202000101.zip XX_20080223000101.zip XX_20080226000101.zip XX_20080227000101.zip XX_20080228000101.zip XX_20080229000101.zip Suppose sysdate = 29 Feb 2007 I need to delete all files... (3 Replies)
Discussion started by: sureshg_sampat
3 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