![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
||||
|
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 $_;
}
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 $_;
}
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.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|