Computing.Net > Forums > Unix > Date sort

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Date sort

Reply to Message Icon

Name: ex
Date: September 25, 2005 at 19:29:15 Pacific
OS: Unix
CPU/Ram: 512
Comment:

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:19

Thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: September 26, 2005 at 05:56:17 Pacific
Reply:

sort -k1,1M -k2n


0

Response Number 2
Name: ex
Date: September 26, 2005 at 16:58:42 Pacific
Reply:

Hi Jim,

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

Thanks.


0

Response Number 3
Name: ex
Date: September 26, 2005 at 19:09:33 Pacific
Reply:

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:19

Thanks and Best Regards.


0

Response Number 4
Name: Jim Boothe
Date: September 27, 2005 at 06:03:14 Pacific
Reply:

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


0

Response Number 5
Name: Jim Boothe
Date: September 27, 2005 at 06:06:30 Pacific
Reply:

Conversion of alphabetical month to numerical month can be done easily enough.

But the posted solution will sort properly by month.


0

Related Posts

See More



Response Number 6
Name: ex
Date: September 28, 2005 at 05:02:52 Pacific
Reply:

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:19

Thanks and Best Regards.


0

Response Number 7
Name: Jim Boothe
Date: September 28, 2005 at 07:08:09 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Date sort

Date Sorting www.computing.net/answers/unix/date-sorting/8166.html

Date sorting in Unix www.computing.net/answers/unix/date-sorting-in-unix/4399.html

Order by Date MM/DD/YY Help Urgent www.computing.net/answers/unix/order-by-date-mmddyy-help-urgent/5576.html