Unix中的排序日期字段 [英] 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
输出应为
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
我已经尝试过使用sort命令对日期进行排序(将date字段作为字符串),但是它没有给出正确的输出.
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.
推荐答案
编年史的解决方案很接近,但是错过了AM/PM的区别,在27-MAR-12 10.28.14.797580000 AM
之前对27-MAR-12 07.28.02.828746000 PM
进行了排序.可以修改:
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.
这篇关于Unix中的排序日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!