bash 使用 unix 工具和多列排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6295710/
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
Sorting with unix tools and multiple columns
提问by josephmisiti
I am looking for the easiest way to solve this problem. I have a huge data set that i cannot load into excel of this type of format
我正在寻找解决此问题的最简单方法。我有一个巨大的数据集,我无法加载到这种格式的 excel 中
This is a sentence|10
This is another sentence|5
This is the last sentence|20
What I want to do is sort this from least to greatest based on the number.
我想要做的是根据数字从最小到最大排序。
cat MyDataSet.txt | tr "|" "\t" | ???
Not sure what the best way is to do this, I was thinking about using awk to switch the columns and the do a sort, but I was having trouble doing it.
不知道最好的方法是什么,我正在考虑使用 awk 来切换列并进行排序,但我在这样做时遇到了麻烦。
Help me out please
请帮帮我
回答by Seth Robertson
sort -t\| -k +2n dataset.txt
Should do it. field separator and alternate key selection
应该做。字段分隔符和备用键选择
回答by Javier C
You usually don't need cat to send the file to a filter. That said, you can use the sortfilter.
您通常不需要 cat 将文件发送到过滤器。也就是说,您可以使用排序过滤器。
sort -t "|" -k 2 -n MyDataSet.txt
This sorts the MyDataSet.txt file using the | character as field separator and sorting numerically according to the second field (the number).
这使用 | 对 MyDataSet.txt 文件进行排序。字符作为字段分隔符并根据第二个字段(数字)进行数字排序。
回答by matchew
have you tried sort -n
你试过 sort -n
$ sort -n inputFile
This is another sentence|5
This is a sentence|10
This is the last sentence|20
you could switch the columns with awk too
你也可以用 awk 切换列
$ awk -F"|" '{print "|"}' inputFile
10|This is a sentence
5|This is another sentence
20|This is the last sentence
combining awk and sort:
结合 awk 和 sort:
$ awk -F"|" '{print "|"}' inputFile | sort -n
5|This is another sentence
10|This is a sentence
20|This is the last sentence
per comments
每条评论
if you have numbers in the sentence
如果句子中有数字
$ sort -n -t"|" -k2 inputFile
This is another sentence|5
This is a sentence|10
This is the last sentence|20
this is a sentence with a number in it 2|22
and of course you could redirect it to a new file:
当然,您可以将其重定向到一个新文件:
$ awk -F"|" '{print "|"}' inputFile | sort -n > outFile
回答by anubhava
Try this sort command:
试试这个排序命令:
sort -n -t '|' -k2 file.txt
回答by zellio
Sort by number, change the separator and grab the second group using sort.
按数字排序,更改分隔符并使用排序获取第二组。
sort -n -t'|' -k2 dataset.txt