Sorting dates in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting dates in Perl
# 1  
Old 06-11-2012
Sorting dates in Perl

I have a directory of backup files.

named like this:
Code:
ldap.data.04-06-2012.tar
ldap.data.03-06-2012.tar
ldap.data.02-06-2012.tar
ldap.data.01-06-2012.tar
ldap.data.31-05-2012.tar
ldap.data.30-05-2012.tar
ldap.data.29-05-2012.tar
ldap.data.28-05-2012.tar
ldap.data.27-05-2012.tar
ldap.data.26-05-2012.tar
ldap.data.25-05-2012.tar
ldap.data.24-05-2012.tar
ldap.data.23-05-2012.tar
ldap.data.22-05-2012.tar
ldap.data.21-05-2012.tar
ldap.data.20-05-2012.tar
ldap.data.19-05-2012.tar
ldap.data.18-05-2012.tar

I have some perl that read this list of file names like this:

Code:
opendir(DIR, "/LDAP_backup");
@FILES= readdir(DIR);


What I get is the list of files in a almost random order.
what I would like is in date order.

I am not a perl programmer, I have been given some code to "fix"


any thoughts?
# 2  
Old 06-11-2012
Can you try like..

Code:
printf '%s\n' *.tar | sort -t_ -k3,4

# 3  
Old 06-11-2012
Quote:
Originally Posted by bmk
Can you try like..

Code:
printf '%s\n' *.tar | sort -t_ -k3,4

I was thinking more of using the bulit in sort and just adding my own code.
a bit like the solution given here: https://www.unix.com/shell-programmin...ings-perl.html

match on year, then month, then day.
compare each as numbers.

I just don't know how to write that in code.
# 4  
Old 06-11-2012
Code:
opendir DIR, "/LDAP_backup";
@files = grep { $_ !~ /^[.]{1,2}$/ } readdir DIR;
closedir DIR;

for (@files) {
    @y = split /[.-]/;
    $x{"$y[4]$y[3]$y[2]"} = $_;
}

for (sort keys %x) {
    print "$x{$_}\n";
}

This User Gave Thanks to balajesuri For This Post:
# 5  
Old 06-11-2012
Try this code.

Code:
#!/usr/bin/perl

use strict;
use warnings;

#---------------------
# Variable definition
#---------------------
my @temp_fl;
my @sort_fl;
my @frst_str;
my @lst_str;

#---------------------
# Input Path definition
#---------------------
my $fl = "C:\\Users\\easkazi\\Desktop\\file.txt";

open (R, $fl) or die ("Cannot open file $fl");
while (<R>){
    chomp;
    my ($k, $l, $m, $n, $o, $p) = split (/(\w+\.\w+\.)(\d\d)-(\d\d)-(\d+)(\.\w+)/,$_);
    push @temp_fl, "$o$n$m";
    push @frst_str, "$l";
    push @lst_str, "$p";  
   
} 

close ($fl);

@sort_fl = sort {$a <=> $b} @temp_fl;

foreach my $rst_fl(@sort_fl){
   chomp($rst_fl);
   my @rslt = split (/(\d\d\d\d)(\d\d)(\d\d)/,$rst_fl);
   print "$frst_str[0]$rslt[3]-$rslt[2]-$rslt[1]$lst_str[0]\n";    
}

###RESULT DATA#######
ldap.data.18-05-2012.tar
ldap.data.19-05-2012.tar
ldap.data.20-05-2012.tar
ldap.data.21-05-2012.tar
ldap.data.22-05-2012.tar
ldap.data.23-05-2012.tar
ldap.data.24-05-2012.tar
ldap.data.25-05-2012.tar
ldap.data.26-05-2012.tar
ldap.data.27-05-2012.tar
ldap.data.28-05-2012.tar
ldap.data.29-05-2012.tar
ldap.data.30-05-2012.tar
ldap.data.31-05-2012.tar
ldap.data.01-06-2012.tar
ldap.data.02-06-2012.tar
ldap.data.03-06-2012.tar
ldap.data.04-06-2012.tar
ldap.data.24-06-2012.tar

# 6  
Old 06-11-2012
ok .. i might be wrong but this looks like 1 file created everyday ? if yes then read on ..

if files are created according to dates then you can use "ls -lrt" and that will sort the file for you according to creation date!

its not good to depend upon external program but "ls" is pretty basic command and usage remains same on almost all the variants of *NIX...
Code:
# latest at bottom 
chomp ( @Files = `ls -1rt <DIR>` ) ;

# latest at top
chomp ( @Files = `ls -1t <DIR>` ) ;

# 7  
Old 06-12-2012
Quote:
Originally Posted by balajesuri
Code:
opendir DIR, "/LDAP_backup";
@files = grep { $_ !~ /^[.]{1,2}$/ } readdir DIR;
closedir DIR;

for (@files) {
    @y = split /[.-]/;
    $x{"$y[4]$y[3]$y[2]"} = $_;
}

for (sort keys %x) {
    print "$x{$_}\n";
}

works like magic.

thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl : difference between two dates in years

hello folks, I have a requirement in which I have to calculate the difference of localdate(today's date) and the given(earlier) date and to check whether the difference is exactly a year or more than that(can be 1 year or 2 years or 3 years.. ) . Could anyone please let me know the logic... (2 Replies)
Discussion started by: scriptscript
2 Replies

2. Shell Programming and Scripting

Perl ::duration of time in between dates

Hello All, I have two strings with date and time as follows.. $starttime= "06/11/2013 "; $starttime= "05:15"; $enddate="06/12/2013"; $endtime="04:45"; dates are in mm/dd/yyyy format and time in military format. and I am looking the duration of time(in minutes) in between dates. ... (3 Replies)
Discussion started by: scriptscript
3 Replies

3. Programming

Perl read file with dates in

Hi All I have a text file that has a list of dates in it ( see below example) is there i can just pull out the lines that are from this week ( week starting on monday) and then work out the how many occurances there are on each name in collum 2 2013-05-13 08:20:02 bacha ... (8 Replies)
Discussion started by: ab52
8 Replies

4. Shell Programming and Scripting

Comparing the dates with the current date in perl scripting

Hi i have a file containg dates likebelow 4/30/2013 3/31/2013 4/30/2013 4/16/2013 4/30/2013 4/30/2013 5/30/2013 5/30/2013 4/30/2013 5/30/2013 5/30/2013 3/31/2013 now i want to compare the above dates with current date and i want to display the difference . (10 Replies)
Discussion started by: siva kumar
10 Replies

5. Shell Programming and Scripting

Subtracting two dates in PERL

Hi guys, First of all, I would like to say this is my first post in the unix.com forums. I am a beginner in PERL and have only started writing my first scripts. With that out of the way, I have a question regarding the calculation of time dates in PERL. I have two scalar variables with the... (1 Reply)
Discussion started by: DiRNiS
1 Replies

6. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

7. Shell Programming and Scripting

Sorting dates in chronological order

Hi forum. I'm hoping someone can help me out with this problem. I tried to search online but couldn't come up with an exact solution. I have the following data file: H|20-May-2011|MF_FF.dat|77164|731374590.96|1|1|731374590.96|76586|77164|578|2988|Y... (8 Replies)
Discussion started by: pchang
8 Replies

8. Shell Programming and Scripting

Perl script to toggle through dates by week

Hi, I need help to toggle through dates on a weekly basis to be fed into a script as inputs. The format should be: yyyy/mm/dd (start) yyyy/mm/dd (end), where end date is 7 days increments. The date (start) would be input as an ARGV and would continue until current date. I can check... (2 Replies)
Discussion started by: subhap
2 Replies

9. Shell Programming and Scripting

Perl difference between dates

Hi, Is there any way I can get the difference between two dates in terms of days? I have used this method so far, but I cant format it in terms of days. @a=&DateCalc($date1,$date2,0); The o/p that I am getting is sort of like this: +0:0:0:4:0:0:0 I just want to get 4 days as an o/p.... (1 Reply)
Discussion started by: King Nothing
1 Replies

10. Shell Programming and Scripting

Help w/ Perl dates

I need to create 12 variables, the first of which is the date of the first day of the current month (01/01/2006), and the remaining 11 are to equal each month after the current. var1 = 01/01/2006 var2 = 02/01/2006 var3 = 03/01/2006 var4 = 04/01/2006 etc. How can I easily do this is in... (7 Replies)
Discussion started by: ssmiths001
7 Replies
Login or Register to Ask a Question