LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-22-2010, 01:21 AM   #1
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Rep: Reputation: 16
Smile Sort as per the Dates and Months


I am getting little bit difficult in sorting the date ranges which are in a field like

$cat date_file.txt
Jan 10
Jan 23
Jan 7
Jan 1
Jan 31
Jan 30
Jan 2
Jan 5
Apr 3
Apr 12
Apr 20
Aug 22

How make a sort as per the Month and date , i mean result should be as per the month and date wise. If i go for the sort -M , i am not able to get the list as per date of the particular month.
 
Old 02-22-2010, 01:39 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
The qn is whether you want them sorted alphabetically then numerically, or properly by date as a human would see them ie Feb comes before Apr.
If the former, you'd start by adding a leading zero to the single digit dates.
If the latter, you'll need a tool that actually understands dates eg MySQL or Perl would prob be able to handle that.
Perl has many modules you can use eg http://search.cpan.org/~stbey/Date-C.../Date/Calc.pod
 
Old 02-22-2010, 03:07 AM   #3
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Original Poster
Rep: Reputation: 16
I have no much idea about the perl and mysql but i know sed and awk . There is any possible way to do using sed and awk to sort the dates of various months .
 
Old 02-22-2010, 03:57 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Code:
$ echo "Jan 10
Jan 23
Jan 7
Jan 1
Jan 31
Jan 30
Jan 2
Jan 5
Apr 3
Apr 12
Apr 20
Aug 22" | sort -k1,1M -k2,2g
Jan 1
Jan 2
Jan 5
Jan 7
Jan 10
Jan 23
Jan 30
Jan 31
Apr 3
Apr 12
Apr 20
Aug 22
$
Looks OK to me?
 
1 members found this post helpful.
Old 02-22-2010, 11:54 PM   #5
dina3e
Member
 
Registered: Mar 2008
Location: Bangalore
Distribution: Enterprise Red Hat linux
Posts: 98

Original Poster
Rep: Reputation: 16
Thanks Tinkster

I got the utilization of the sort option especially -M for sort of Months.

One more thing, this code( sort -k1M -k2g ) also work properly what the need of using the 1,1M and 2,2g in the previous code.
Any way How could we sort the weekdays , Mean- Sun ,Mon , Tue....Sat. if any file containing .
 
  


Reply

Tags
sort


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I use GNU sort to sort one field in order, another in reverse? zombieite Linux - Newbie 4 04-27-2009 01:23 AM
php sort help - sort numerical descending then by alphabetical? RavenLX Programming 3 03-11-2009 09:35 AM
How do I do filtering in Perl (keep sort order and sort again by another means)? RavenLX Programming 9 12-19-2008 11:12 AM
selection sort compiles but does not sort the array as desired ganesha Programming 2 04-20-2008 08:44 AM
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 09:40 PM


All times are GMT -5. The time now is 04:25 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration