bash 使用bash按第5列对csv文件进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18642188/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:28:36  来源:igfitidea点击:

Sorting csv file by 5th column using bash

linuxbash

提问by mrm9084

The file looks like

该文件看起来像

5.1,3.5,1.4,0.2,Banana
4.9,3.0,1.4,0.6,Apple
4.8,2.8,1.3,1.2,Apple

and I need to have it be

我需要它

4.9,3.0,1.4,0.2,Apple
4.8,2.8,1.3,1.2,Apple
5.1,3.5,1.4,0.2,Banana

I have been trying to use

我一直在尝试使用

sort -t, -k5 file.csv > sorted.csv

All it does is make it

它所做的就是让它

5.1,3.5,1.4,0.2,Banana
4.8,2.8,1.3,1.2,Apple
4.9,3.0,1.4,0.6,Apple

How do I make it like this? It does not seem to be sorting it at all.

我如何做到这一点?它似乎根本没有对其进行排序。

回答by XHunter Hunter

Is this what you need to have it be

这是你需要的吗

# sort -t . -nrk2 sorted.csv 
4.9,3.0,1.4,0.6,Apple
4.8,2.8,1.3,1.2,Apple
5.1,3.5,1.4,0.2,Banana

回答by BigSmoke

GNU sort is locale sensitive, which can cause weirdness. Try the following and see if it makes a difference:

GNU 排序对区域设置敏感,这可能会导致异常。尝试以下操作,看看它是否有所作为:

LC_ALL=C sort -t, -k5 file.csv > sorted.csv