bash 在 unix 中对日期字段进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/11536368/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting date field in unix
提问by user1011046
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.
我已经尝试使用 sort 命令对日期进行排序(将日期字段作为字符串),但它没有给出正确的输出。
回答by William Pursell
Chronicle's solution is close, but misses the AM/PM distinction, sorting 27-MAR-12 07.28.02.828746000 PMbefore 27-MAR-12 10.28.14.797580000 AM.  This can be modified:
Chronicle 的解决方案很接近,但错过了 AM/PM 的区别,排序27-MAR-12 07.28.02.828746000 PM在27-MAR-12 10.28.14.797580000 AM. 这可以修改:
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.
但这仍然非常脆弱。将日期转换为纪元时间并进行数字比较会好得多。
回答by Debaditya
Try this :
尝试这个 :
Input.txt
输入.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
回答by Andrey
This script sorts by Epoch time with nanosecond resolution:
此脚本以纳秒分辨率按纪元时间排序:
awk '{
  t = gensub(/\.([0-9]{2})\./, ":\1:", 1, while read a; do   
grep "^${a}" input.txt; 
done < <(sed 's/\./:/;s/\./:/' input.txt | xargs -n3 -I{} date -d"{}" +%s | sort | xargs -n1 -I{} date -d @'{}' +'%d-%^h-%y %I.%M.%S')
);
  command = "date +%s%N -d \x022" t "\x022";
  command | getline t;
  close(command);
  print t, ##代码##;
}' unsorted.txt | sort -n -k 1 | cut -d ' ' -f 2- > sorted.txt
回答by Andrey
You could use date, which is generally probably a decent idea, especially if you don't need to worry about the microseconds, otherwise you could probably clip the microseconds off and sort that as a secondary sorting field.
您可以使用日期,这通常可能是一个不错的主意,特别是如果您不需要担心微秒,否则您可能会剪掉微秒并将其作为辅助排序字段进行排序。
##代码##
