Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting date field in unix

I have text file which contains hundreds of thousands of records. One of the fields is a date field. Is there is any way to sort the file based on the date field?

09-APR-12 04.08.43.632279000 AM
19-MAR-12 03.53.38.189606000 PM
19-MAR-12 03.56.27.933365000 PM
19-MAR-12 04.00.13.387316000 PM
19-MAR-12 04.04.45.168361000 PM
19-MAR-12 03.54.32.595348000 PM
27-MAR-12 10.28.14.797580000 AM
28-MAR-12 12.28.02.652969000 AM
27-MAR-12 07.28.02.828746000 PM

The Output should come as

19-MAR-12 03.53.38.189606000 PM
19-MAR-12 03.54.32.595348000 PM
19-MAR-12 03.56.27.933365000 PM
19-MAR-12 04.00.13.387316000 PM
19-MAR-12 04.04.45.168361000 PM
27-MAR-12 10.28.14.797580000 AM
27-MAR-12 07.28.02.828746000 PM
28-MAR-12 12.28.02.652969000 AM
09-APR-12 04.08.43.632279000 AM

I have tried the sort command to order the date (taking the date field as a string), but it is not giving the correct output.

like image 626
user1011046 Avatar asked Dec 20 '22 18:12

user1011046


2 Answers

Chronicle's solution is close, but misses the AM/PM distinction, sorting 27-MAR-12 07.28.02.828746000 PM before 27-MAR-12 10.28.14.797580000 AM. This can be modified:

sort -t- -k 3.1,3.2 -k 2M -k 1n -k 3.23,3.24

But that is still very fragile. It would be much better to convert the dates to an epoch time and compare numerically.

like image 132
William Pursell Avatar answered Jan 06 '23 00:01

William Pursell


Try this :

Input.txt

09-APR-12 04.08.43.632279000 AM 
19-MAR-12 03.53.38.189606000 PM 
19-MAR-12 03.56.27.933365000 PM 
19-MAR-12 04.00.13.387316000 PM 
19-MAR-12 04.04.45.168361000 PM 
19-MAR-12 03.54.32.595348000 PM 
27-MAR-12 10.28.14.797580000 AM 
28-MAR-12 12.28.02.652969000 AM 
27-MAR-12 07.28.02.828746000 PM 

Code

 sort -t "-"  -k 3 -k 2M -nk 1 Input.txt

Output

19-MAR-12 03.53.38.189606000 PM
19-MAR-12 03.54.32.595348000 PM
19-MAR-12 03.56.27.933365000 PM
19-MAR-12 04.00.13.387316000 PM
19-MAR-12 04.04.45.168361000 PM
27-MAR-12 07.28.02.828746000 PM
27-MAR-12 10.28.14.797580000 AM
28-MAR-12 12.28.02.652969000 AM
09-APR-12 04.08.43.632279000 AM
like image 22
Debaditya Avatar answered Jan 06 '23 02:01

Debaditya