Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
How do I sort something like that in date order from earliest to oldest.
Jun 10 03:22:21
Jun 12 14:07:38
Jul 9 16:28:25
Jun 9 16:28:25
Jun 9 16:29:29
Jun 9 16:30:23
Jun 9 16:32:19Thanks.

Hi Jim,
Can you explain to me this line works? basically you sort by month first, then day then time is it?
Thanks.

Hi Gurus,
Isit possible to do a straight conversion for the first column from Jun to for example value 6 and Jul to value 7?
Jun 10 03:22:21
Jun 12 14:07:38
Jul 9 16:28:25
Jun 9 16:28:25
Jun 9 16:29:29
Jun 9 16:30:23
Jun 9 16:32:19Thanks and Best Regards.

sort -k1,1M -k2n
That says the first level of sorting is to use fields 1 through 1, and the sort should not be alphanumerical, (Apr, Aug, etc), but chronological (Jan, Feb, etc).
And the second level of sort should be fields 2 through end-of-line (because I did not specify an ending field here), and the sort should not be alphanumerical (1,10,2,3,etc), but rather numerical (1,2,3,10).
If your column alignment is consistent (as in your example), you should not need to specify a third sort level. But if alignment is not consistent, you would need a third sort key:
sort -k1,1M -k2n -k3b

Conversion of alphabetical month to numerical month can be done easily enough.
But the posted solution will sort properly by month.

How can I achieve the conversion of alphabetical month to numerical month for the above example? Would be easier if I am able to do that.
Jun 10 03:22:21
Jun 12 14:07:38
Jul 9 16:28:25
Jun 9 16:28:25
Jun 9 16:29:29
Jun 9 16:30:23
Jun 9 16:32:19Thanks and Best Regards.

The month can be converted using basic shell commands, or awk or sed. In the sed solution below, the search pattern is anchored to start-of-line to avoid changing anything further into the line.
sed '
s/^Jan/01/
s/^Feb/02/
s/^Mar/03/
s/^Apr/04/
s/^May/05/
s/^Jun/06/
s/^Jul/07/
s/^Aug/08/
s/^Sep/09/
s/^Oct/10/
s/^Nov/11/
s/^Dec/12/
' myfile

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |