LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Sort as per the Dates and Months (https://www.linuxquestions.org/questions/linux-newbie-8/sort-as-per-the-dates-and-months-790692/)

dina3e 02-22-2010 12:21 AM

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.

chrism01 02-22-2010 12:39 AM

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

dina3e 02-22-2010 02:07 AM

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 .

Tinkster 02-22-2010 02:57 AM

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?

dina3e 02-22-2010 10:54 PM

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 .


All times are GMT -5. The time now is 02:56 PM.