通过Linux排序日期“排序”命令

时间:2017-10-02 04:50:35

标签: linux sorting sed

我已经对下面文本的第5和第6列(纬度/经度对)进行了排序,并尝试按如下方式对第2列进行排序:

sort -t, -k5 -V -k6 -V -rk2 -V file.txt 

以下日期很接近,但仍然略有不合规格。我将如何按顺序正确排序?

"1979-01-02 06:00:00","1979-01-02 06:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-01 00:00:00","1979-01-01 00:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-01 06:00:00","1979-01-01 06:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-01 12:00:00","1979-01-01 12:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-01 18:00:00","1979-01-01 18:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-02 00:00:00","1979-01-02 00:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-02 06:00:00","1979-01-02 06:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 00:00:00","1979-01-01 00:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 06:00:00","1979-01-01 06:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 12:00:00","1979-01-01 12:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 18:00:00","1979-01-01 18:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-02 00:00:00","1979-01-02 00:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-02 06:00:00","1979-01-02 06:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 00:00:00","1979-01-01 00:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 06:00:00","1979-01-01 06:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 12:00:00","1979-01-01 12:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 18:00:00","1979-01-01 18:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-02 00:00:00","1979-01-02 00:00:00","XXX","YYY",-80,49,ZZZ

编辑:我正在尝试实现以下结果: 请注意,最终结果包含按时间顺序在各自的纬度和经度范围内组合在一起的1979-01-01和1979-01-02行:

"1979-01-01 00:00:00","1979-01-01 00:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-01 06:00:00","1979-01-01 06:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-01 12:00:00","1979-01-01 12:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-01 18:00:00","1979-01-01 18:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-02 00:00:00","1979-01-02 00:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-02 06:00:00","1979-01-02 06:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-01 00:00:00","1979-01-01 00:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 06:00:00","1979-01-01 06:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 12:00:00","1979-01-01 12:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 18:00:00","1979-01-01 18:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-02 00:00:00","1979-01-02 00:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-02 06:00:00","1979-01-02 06:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 00:00:00","1979-01-01 00:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 06:00:00","1979-01-01 06:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 12:00:00","1979-01-01 12:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 18:00:00","1979-01-01 18:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-02 00:00:00","1979-01-02 00:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-02 06:00:00","1979-01-02 06:00:00","XXX","YYY",-80,49,ZZZ

提前谢谢你。

1 个答案:

答案 0 :(得分:1)

如果我明白你想要什么,看起来你正在过度思考sort。你可以使用类似的东西:

$ sort -t, -k6nr -k5 < file

示例输入

$ cat dat/a2.txt
"1979-01-02 06:00:00","1979-01-02 06:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 06:00:00","1979-01-01 06:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-02 00:00:00","1979-01-02 00:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-01 18:00:00","1979-01-01 18:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-01 18:00:00","1979-01-01 18:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-02 00:00:00","1979-01-02 00:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 12:00:00","1979-01-01 12:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-02 06:00:00","1979-01-02 06:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-02 00:00:00","1979-01-02 00:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 00:00:00","1979-01-01 00:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 00:00:00","1979-01-01 00:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 12:00:00","1979-01-01 12:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 12:00:00","1979-01-01 12:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 18:00:00","1979-01-01 18:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 06:00:00","1979-01-01 06:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 06:00:00","1979-01-01 06:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 00:00:00","1979-01-01 00:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-02 06:00:00","1979-01-02 06:00:00","XXX","YYY",-80,50,ZZZ

示例使用/输出

$ sort -t, -k6nr -k5 < dat/a2.txt
"1979-01-01 00:00:00","1979-01-01 00:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-01 06:00:00","1979-01-01 06:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-01 12:00:00","1979-01-01 12:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-01 18:00:00","1979-01-01 18:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-02 00:00:00","1979-01-02 00:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-02 06:00:00","1979-01-02 06:00:00","XXX","YYY",-80,50,ZZZ
"1979-01-01 00:00:00","1979-01-01 00:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 06:00:00","1979-01-01 06:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 12:00:00","1979-01-01 12:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 18:00:00","1979-01-01 18:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-02 00:00:00","1979-01-02 00:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-02 06:00:00","1979-01-02 06:00:00","XXX","YYY",-80,49.5,ZZZ
"1979-01-01 00:00:00","1979-01-01 00:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 06:00:00","1979-01-01 06:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 12:00:00","1979-01-01 12:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-01 18:00:00","1979-01-01 18:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-02 00:00:00","1979-01-02 00:00:00","XXX","YYY",-80,49,ZZZ
"1979-01-02 06:00:00","1979-01-02 06:00:00","XXX","YYY",-80,49,ZZZ

如果我理解您要实现的排序顺序,请告诉我。如果没有,我很乐意进一步帮助。