bash 根据第一列对 CSV 文件进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26249209/
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
Sort CSV file based on first column
提问by fiddle
Is there a way to sort a csv file based on the 1st column using some shell command?
有没有办法使用某些 shell 命令根据第一列对 csv 文件进行排序?
I have this huge file with more than 150k lines hence I can do it in excel:( is there an alternate way ?
我有这个超过 15 万行的大文件,因此我可以在 excel 中完成:(有没有其他方法?
回答by Travis
sort -k1 -n -t, filename
should do the trick.
sort -k1 -n -t, filename
应该做的伎俩。
-k1
sorts by column 1.
-k1
按第 1 列排序。
-n
sorts numerically instead of lexicographically (so "11" will not come before "2,3...").
-n
按数字排序而不是按字典排序(因此“11”不会出现在“2,3...”之前)。
-t,
sets the delimiter (what separates values in your file) to ,
since your file is comma-separated.
-t,
将分隔符(用于分隔文件中的值)设置为,,
因为您的文件是逗号分隔的。
回答by Bharthan
I don't know why above solution was not working in my case.
我不知道为什么上述解决方案在我的情况下不起作用。
15,5
17,2
18,6
19,4
8,25
8,90
9,47
9,49
10,67
10,90
13,96
159,9
however this command solved my problem.
但是这个命令解决了我的问题。
sort -t"," -k1n,1 fileName
回答by Joshua Pinter
Using csvsort
.
使用csvsort
.
Install
csvkit
if not already installed.brew install csvkit
Sort CSV by first column.
csvsort -c 1 original.csv > sorted.csv
安装
csvkit
如果尚未安装。brew install csvkit
按第一列对 CSV 进行排序。
csvsort -c 1 original.csv > sorted.csv