Sorting files based on timestamp and picking the latest file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sorting files based on timestamp and picking the latest file
# 1  
Old 05-17-2012
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

Please help me in resolving or any other script.

Moderator's Comments:
Mod Comment Please use code tags for blocks of data


---------- Post updated 05-17-12 at 01:59 AM ---------- Previous update was 05-16-12 at 10:40 PM ----------

Hi all,
Need your help in fixing the issue....

Last edited by jim mcnamara; 05-17-2012 at 12:49 AM..
# 2  
Old 05-17-2012
Code:
# printf '%s\n' *.dat* | sort -t. -k3,4|\
awk -F'.' '{f[xx++]=$0}p!=$3{a[x++]=$3;p=$3}
END{if(S>x)S=x;if(S<=x)while(j++!=S){x--;b[j]=b[j]?b"\n"a[x]:a[x]}for(i=j-1;i>=1;--i)for(jj=0;jj<xx;jj++)if(match(f[jj],b[i]))print f[jj]}' S=4
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

regards
ygemici

Last edited by ygemici; 05-17-2012 at 04:48 AM..
# 3  
Old 05-17-2012
Hi,
Can you please explain in detail if possible?
i have just copy pasted the script! and i was unable to run!

Last edited by robertbrown624; 05-17-2012 at 05:25 AM..
# 4  
Old 05-17-2012
i have just copy pasted the script! and i was unable to run!

what error, and how you ran and what is your OS and shell ?
# 5  
Old 05-17-2012
Hi,
Currently i wrote the script in this way (im a newbie)
Code:
#!/bin/ksh
awk -F'.' '{f[xx++]=$0}p!=$3{a[x++]=$3;p=$3}'
END
{
while(j++!=S){x--;b[j]=b[j]?b"\n"a[x]:a[x]
}
for(i=j-1;i>=1;--i)
for(jj=0;jj<xx;jj++)
if(match(f[jj],b[i]))
print f[jj]}' S=4

Running in AIX

Last edited by Scrutinizer; 05-17-2012 at 06:18 AM.. Reason: code tags
# 6  
Old 05-17-2012
you should not close the single quote

Code:
 
awk -F'.' '{f[xx++]=$0}p!=$3{a[x++]=$3;p=$3}'

# 7  
Old 05-17-2012
Hi,
Tried debugging but could not solve
Code:
$ . ./test1.ksh .
+ . ./test1.ksh .
+ awk -F. {f[xx++]=$0}p!=$3{a[x++]=$3;p=$3}
END
{
while(j++!=S){x--;b[j]=b[j]?b"\n"a[x]:a[x]
}
for(i=j-1;i>=1;--i)
for(jj=0;jj<xx;jj++)
if(match(f[jj],b[i]))
print f[jj]} S=4
 syntax error The source line is 3.
 The error context is
                END >>> 
 <<< 
 awk: Quitting
 The source line is 3.


Last edited by Scrutinizer; 05-17-2012 at 06:50 AM..
 
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

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

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

8. Shell Programming and Scripting

sorting of files on the basis of timestamp

Hi, With the help of below script im able to get the count of all the .xml files but that count is not specific to a day ie its the total count of all .xml files what i want is specific to 1 day and that of every half an hr ie from 23 feb 2009 7 am till 23rd feb 2009 2300 am and from 07:00 to... (1 Reply)
Discussion started by: ss_ss
1 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