如何按特定列对csv进行排序

时间:2017-06-25 08:56:42

标签: bash shell csv sorting

我尝试在第4列对csv进行排序。

Sort -n -k4 temperature.csv

结果我得到了这个:

2017-06-24 11:20,23.57,19.0,16.7,0.087,3.615
2017-06-24 11:25,23.51,19.0,16.7,0.087,3.689
2017-06-24 12:45,22.03,19.0,17.1,0.096,4.152
2017-06-24 13:00,21.92,19.0,17.1,0.096,4.229
2017-06-24 14:00,22.22,19.0,17.4,0.197,4.639
2017-06-24 14:25,22.21,19.0,17.5,0.197,4.774
2017-06-24 15:10,22.30,19.0,17.1,0.134,5.472
2017-06-24 16:00,22.42,19.0,17.3,0.134,5.93
2017-06-24 17:45,22.07,21.0,17.0,0.144,6.472
2017-06-24 18:25,21.90,21.0,16.9,0.15,6.814
2017-06-24 19:40,23.01,21.0,16.9,0.318,8.503

正如您所看到的,第4列未正确排序。我希望第一行是17.5,最后一行是16.7。

我也试过这个:

sort -n -t. -k4,1n temperature.csv

结果与前一个示例完全相同。 谁能给我一个提示?

1 个答案:

答案 0 :(得分:4)

使用以下sort命令:

sort -t, -k4,4 -nr temperature.csv

输出:

2017-06-24 14:25,22.21,19.0,17.5,0.197,4.774
2017-06-24 14:00,22.22,19.0,17.4,0.197,4.639
2017-06-24 16:00,22.42,19.0,17.3,0.134,5.93
2017-06-24 15:10,22.30,19.0,17.1,0.134,5.472
2017-06-24 13:00,21.92,19.0,17.1,0.096,4.229
2017-06-24 12:45,22.03,19.0,17.1,0.096,4.152
2017-06-24 17:45,22.07,21.0,17.0,0.144,6.472
2017-06-24 19:40,23.01,21.0,16.9,0.318,8.503
2017-06-24 18:25,21.90,21.0,16.9,0.15,6.814
2017-06-24 11:25,23.51,19.0,16.7,0.087,3.689
2017-06-24 11:20,23.57,19.0,16.7,0.087,3.615
  • -t, - 字段分隔符

  • -k4,4 - 仅按第4个字段排序

  • -nr - 按相反顺序按数字排序