LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 06-03-2004, 07:45 AM   #1
subu_s
Member
 
Registered: Jun 2004
Posts: 36

Rep: Reputation: 15
Sorting a file containing dates


Hi,
I have a file which has data in the following format:-
15-dec-1992
11-nov-1992
26-apr-1993
15-mar-1993
19-jan-1993
05-oct-1990
21-jul-1992
03-dec-1991
15-feb-1991
22-jul-1993

I want to sort the dates in the reverse chronological order.

Please help providing a sample script or command.

Regards,
Subramanian S
 
Old 06-03-2004, 10:42 AM   #2
ranger_nemo
Senior Member
 
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
Here's a quicky in Python...
Code:
months = { "jan":"A", "feb":"B", "mar":"C", "apr":"D", "may":"E", "jun":"F", "jul":"G", "aug":"H", "sep":"I", "oct":"J", "nov":"K", "dec":"L" }

temp = []
for entry in input:
  if entry <> "":
    splits = entry.split("-")
    outry = [splits[2] + months[splits[1]] + splits[0] , entry]
    temp.append(outry)

temp.sort()
temp.reverse()

output = []
for entry in temp:
  output.append(entry[1])

print output
I'll leave it up to you to work it into a real script with "input = infile.readlines()" and "outfile.writelines(output)" parts. It relys heavily on the input being formatted the same... double-digit day hyphen lower-case three-letter month hyphen four-digit year
 
Old 06-03-2004, 02:07 PM   #3
phaserx
LQ Newbie
 
Registered: Jun 2004
Location: The city of Lost Angels
Distribution: RedHat and Gentoo
Posts: 21

Rep: Reputation: 15
You could even just do something as simple as (where "dates" is the name of the file containing all the dates you want to sort):

awk -F- '{print $3"-"$2"-"$1}' dates | sort -r | awk -F- '{print $3"-"$2"-"$1}'

That will take this exact format:

15-dec-1992
11-nov-1992
26-apr-1993
15-mar-1993
19-jan-1993
05-oct-1990
21-jul-1992
03-dec-1991
15-feb-1991
22-jul-1993

And output it to look like this:

15-mar-1993
22-jul-1993
19-jan-1993
26-apr-1993
11-nov-1992
21-jul-1992
15-dec-1992
15-feb-1991
03-dec-1991
05-oct-1990
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
file created/accessed dates plainkeyman Linux - General 4 10-27-2005 08:34 PM
Trouble working with file dates cboxall Linux - Newbie 2 07-21-2005 12:11 PM
file dates digitalgravy Linux - General 2 01-20-2005 11:58 AM
File Dates Rv5 Programming 5 09-07-2004 06:59 PM
File dates. vexer Programming 5 04-28-2004 01:14 AM


All times are GMT -5. The time now is 03:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration