Date – How to Sort Lines According to Day

datesort

I have a text file in following format, and I need to sort the lines according to date. Is there some simple way to do it (preferably in bash) ?

2013-May-30     2
2013-May-21     10
2013-Jun-27     8
2013-Jun-18     9
2013-Jun-09     17
2013-May-20     21
2013-Jun-10     1
2013-Jun-01     2
2013-Aug-09     6
2013-Aug-08     5
2013-Aug-07     2
...

Best Answer

If you tell GNU sort to split the fields by a different character, a dash - in your case it's pretty easy to sort this:

$ sort -n -t"-" -k1 -k2M -k3 file.txt

Example

$ sort -n -t"-" -k1 -k2M -k3 file.txt
2013-May-20     21
2013-May-21     10
2013-May-30     2
2013-Jun-01     2
2013-Jun-09     17
2013-Jun-10     1
2013-Jun-18     9
2013-Jun-27     8
2013-Aug-07     2
2013-Aug-08     5
2013-Aug-09     6

Reference

Related Question