The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to sort a field in a file having date values risshanth Shell Programming and Scripting 4 06-04-2008 05:03 AM
Perl: Extracting date from file name and comparing with current date MKNENI Shell Programming and Scripting 4 03-26-2008 04:01 PM
how to sort paragraphs by date within a file nabmufti Shell Programming and Scripting 1 02-13-2008 05:33 PM
Perl Sort on Text File eltinator Shell Programming and Scripting 6 08-07-2007 02:20 PM
Sort file in perl annececile Shell Programming and Scripting 4 06-21-2002 08:52 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 4.00 average. Display Modes
  #1 (permalink)  
Old 04-14-2005
esham's Avatar
esham esham is offline
Registered User
  
 

Join Date: Nov 2004
Location: INDIA
Posts: 107
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 (permalink)  
Old 04-14-2005
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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 (permalink)  
Old 04-14-2005
esham's Avatar
esham esham is offline
Registered User
  
 

Join Date: Nov 2004
Location: INDIA
Posts: 107
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 (permalink)  
Old 04-14-2005
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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 09:09 AM..
  #5 (permalink)  
Old 04-14-2005
esham's Avatar
esham esham is offline
Registered User
  
 

Join Date: Nov 2004
Location: INDIA
Posts: 107
Thanks a lot for that great reply..
Trying to learn it..what each lines does..

cheers..
esham
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:26 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0