Linux 如何使用内置的“排序”程序同时按两个字段(一个数字,一个字符串)排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5206033/
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
How to sort by two fields (one numeric, one string) at the same time using the built in "sort" program?
提问by Vijay
I have a file, lets say "bigfile", with tabular data of the following form,
我有一个文件,可以说“大文件”,其中包含以下形式的表格数据,
a1 b2 a3 1
b1 a2 c3 0
... and so on.
a1 b2 a3 1
b1 a2 c3 0
...等等。
I want to use the built-in "sort" program on my Linux machine so sort this file by the fourth field(numeric) and then by the first field at the same time. I went through the man pages a couple of times and all I could come up with was,
我想在我的 Linux 机器上使用内置的“排序”程序,所以同时按第四个字段(数字)和第一个字段对这个文件进行排序。我翻阅了几次手册页,我能想到的是,
sort -n -k4,4 -k1,1 bigfile
Is there a way to make "sort" do what I want or I have to write my own custom program?
有没有办法让“排序”做我想做的事,或者我必须编写自己的自定义程序?
Thank you.
谢谢你。
采纳答案by wnoise
From the manpage:
从联机帮助页:
POS is F[.C][OPTS], where F is the field number and C the character position in the field; both are origin 1. If neither -t nor -b is in effect, characters in a field are counted from the beginning of the preceding whitespace. OPTS is one or more single-letter ordering options, which override global ordering options for that key. If no key is given, use the entire line as the key.
POS 为 F[.C][OPTS],其中 F 为字段编号,C 为字段中的字符位置;两者都是原点 1。如果 -t 和 -b 都无效,则字段中的字符从前一个空格的开头开始计数。OPTS 是一个或多个单字母排序选项,它会覆盖该键的全局排序选项。如果没有给出键,则使用整行作为键。
sort -k4,4n -k1,1 bigfile
ought to do it.
sort -k4,4n -k1,1 bigfile
应该这样做。
Another option would be sort -k1,1 bigfile | sort --stable -n -k4,4
The stable sort means that ties on the 4th field are resolved by the initial position, which is set by the first pass of sort to be first field.
另一个选项是sort -k1,1 bigfile | sort --stable -n -k4,4
稳定排序意味着第 4 个字段上的关系由初始位置解决,该位置由第一遍排序设置为第一个字段。