如何按特定列对csv进行排序 [英] How to sort csv by specific column
本文介绍了如何按特定列对csv进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试按第4列对包含csv的温度进行排序。
I try to sort a csv contain temperatures by the 4th column.
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。
As you can see the 4th column isn't sorted correctly. I would expect 17.5 in tbe first line and 16.7 in the last line.
我也尝试过这样做:
sort -n -t. -k4,1n temperature.csv
结果与前面的示例完全相同。
有人可以给我提示吗?
The result is exactly the same as the previous example. Can anyone give me a hint?
推荐答案
使用以下 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个字段排序-k4,4
- sort by 4th field only-nr
-以相反的顺序对数字进行排序-nr
- sort numerically in reverse order这篇关于如何按特定列对csv进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文