开发者

How to sort by two fields (one numeric, one string) at the same time using the built in "sort" program?

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.

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 c开发者_StackOverflow中文版ome up with was,

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.


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.

sort -k4,4n -k1,1 bigfile ought to do it.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜