I have a file, lets say "bigfile", with tabular data of the following form,
我有一个文件,让我们说“bigfile”,使用以下表格的表格数据,
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.
谢谢。
1 个解决方案
#1
27
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个字段上的连接由初始位置解析,初始位置由排序的第一个通道设置为第一个字段。
#1
27
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个字段上的连接由初始位置解析,初始位置由排序的第一个通道设置为第一个字段。