Take the tour ×

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.

share|improve this question
what was wrong with the -k4? –  Orbit Mar 5 '11 at 18:59
That is not working? –  Zimbabao Mar 5 '11 at 19:02
add comment

1 Answer

up vote 11 down vote accepted

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.

share|improve this answer
Yes. That works. Thank you. –  Vijay Mar 7 '11 at 4:14
add comment

Your Answer


