Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 come 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.

like image 363
Vijay Avatar asked Mar 05 '11 18:03

Vijay


1 Answers

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.

like image 64
wnoise Avatar answered Oct 19 '22 15:10

wnoise