sort a file by date using perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sort a file by date using perl
# 1  
Old 04-14-2005
sort a file by date using perl

Hello,

do any body help me to sort a file by date using perl?

thanks in advance

Esham
# 2  
Old 04-14-2005
What do you mean by 'sort a file by date'?

(1) You have a file with its content with some dates and you want to sort them?

(2) You have a set of files and you would like to sort the list of files by date?
# 3  
Old 04-14-2005
Hello,

I am having the first case... viz..

(1) You have a file with its content with some dates and you want to sort them?

Please help...

Regards
Esham
# 4  
Old 04-14-2005
How to do exactly depends on what your file contains.

For instance, taking the simplest case that the file itself contains just dates but not other fields, then it is pretty straightforward (assuming the file is not extremely large):

Code:
use Date::Parse;

open FILE, "<MyDates.txt";
my @dates = <FILE>;
my @sorted_dates = sort {
    str2time($a) <=> str2time($b)
} @dates;
foreach (@sorted_dates) {
    print $_;
}

Input File:
21 Dec 2005
23 Oct 2002
8 June 2004

Output:
23 Oct 2002
8 June 2004
21 Dec 2005

If the date is a certain column of the file, you may need to do some data extraction in the sort() block. How to do depends on the structure of your file. This is just a simple example to illustrate the idea.

Code:
use Date::Parse;

open FILE, "<MyDates2.txt";
my @dates = <FILE>;
my @sorted_dates = sort {
 my ($d1, $d2) = map { /^(.+):.+$/ && $1 } ($a, $b);
    str2time($d1) <=> str2time($d2)
} @dates;
foreach (@sorted_dates) {
    print $_;
}

Input file:
21 Dec 2005:Event A
23 Oct 2002:Event B
8 June 2004:Event C

Output:
23 Oct 2002:Event B
8 June 2004:Event C
21 Dec 2005:Event A

Last edited by cbkihong; 04-14-2005 at 10:09 AM..
# 5  
Old 04-14-2005
Thanks a lot for that great reply..
Trying to learn it..what each lines does..

cheers..
esham
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort help: How to sort collected 'file list' by date stamp :

Hi Experts, I have a filelist collected from another server , now want to sort the output using date/time stamp filed. - Filed 6, 7,8 are showing the date/time/stamp. Here is the input: #---------------------------------------------------------------------- -rw------- 1 root ... (3 Replies)
Discussion started by: rveri
3 Replies

2. Shell Programming and Scripting

shell script to sort entries in a file by date and time

Hello All, Need a shell script to sort entries in a file by date and time. Below are the entries in the file, i need to sort it first by the date and then time Note :- Date is in MM/DD/YY format and date comes as the 6th & time comes on 7th coloumns respectively. 150 pbnawldb001-b... (10 Replies)
Discussion started by: ajiwww
10 Replies

3. Shell Programming and Scripting

Sort content of text file based on date?

I now have a 230,000+ lines long text file formatted in segments like this: Is there a way to sort this file to have everything in chronological order, based on the date and time in the text? In this example, I would like the result to be: (19 Replies)
Discussion started by: KidCactus
19 Replies

4. Shell Programming and Scripting

How to sort a field in a file having date values

Hi All, I am having a pipe delimited file .In this file the 3rd column is having date values.I need to get the min date and max date from that file. I have used cut -d '|' test.dat -f 3|sort -u But it is not sorting the date .How to sort the date column using unix commands Thanks ... (4 Replies)
Discussion started by: risshanth
4 Replies

5. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies

6. Shell Programming and Scripting

how to sort paragraphs by date within a file

hi all i want help in sortng date in paragraphs within file , i want to ask as if there any option to sort a certain pattern of file not the rest of file.i.e the data of file become sorted with respect to date i have a log file as follows !! *A0628/081 /08-01-10/13 H... (1 Reply)
Discussion started by: nabmufti
1 Replies

7. AIX

loop through the directory for files and sort by date and process the first file

hello i have a requirement where i have a direcotry in which i get files in the format STOCKS.20080114.dat STOCKS.20080115.dat STOCKS.20080117.dat STOCKS.20080118.dat i need to loop through the directory and sort by create date descending order and i need to process the first file. ... (1 Reply)
Discussion started by: dsdev_123
1 Replies

8. Shell Programming and Scripting

Perl Sort on Text File

Hi, I have a file of names and I want perl to do a sort on this file. How can I sort this list of names using perl? I'm thinking of a command like: @sorted = sort { lc($a) cmp lc($b) } @not_sorted # alphabetical sort The only thing I'm sort of unsure of is, how would I get the name in my... (6 Replies)
Discussion started by: eltinator
6 Replies

9. Shell Programming and Scripting

Perl find::file can I sort the out put

Perl file::find can I sort the out put I am using file::find in my script but how I wish to process each file found in date order. Can I sort this module? eg part of current script is.... use File::Find; # Recursively find all files and directories in $mqueue_directory find(\&wanted,... (2 Replies)
Discussion started by: Andrek
2 Replies

10. Shell Programming and Scripting

Sort file in perl

Hi, I have an entry file for a perl script from which I need to remove duplicate entry. For example: one:two:three one:four:five two:one:three must become : one:two:three two:one:three The duplicate entry is only the first field. I try many options of sort system command but don't... (4 Replies)
Discussion started by: annececile
4 Replies
Login or Register to Ask a Question