Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have an issue sorting a file based on the first two columns.

The layout of the file is:

1 998688068 PizzaFan Insurance 22.47
5 072821325 Plaisio Computers 26.35
4 998688068 PizzaFan Food 27.32
5 456834578 G.Yannopoulos Medical 91.67

.... ....

I used this command : sort -n -k 1,2 "$fpath" -o "$fpath.ordered"

The sort result is:

1 473151252 Goodys Food 7.15
1 951515524 Atlantic SuperMarket 41.32
1 998688068 Atlantic SuperMarket 80.23
1 998688068 PizzaFan Food 61.72
1 998688068 PizzaFan Insurance 22.47
2 094321587 Vasilopoulos SuperMarket 6.50

.... ....

I don't understand why all columns get sorted (see 3rd column & PizzaFan Insurance)

I think -k 1,2 means sort on column 1 and resolve ties with column 2, but it's like it doesn't work.

It's the same as using the: sort -n "$fpath" -o "$fpath.ordered"

share|improve this question

migrated from Apr 28 '11 at 15:45

This question came from our site for professional and enthusiast programmers.

kalispera file :) Goodys gia panta :P – SpyrosP Apr 23 '11 at 19:16
I don't understand why sort made entries appear and disappear. Can you post the whole file or show a shorter example? – drysdam Apr 23 '11 at 19:24
It is just a portion of the file entries don't dissappear they are just sorted, but I didn't post the whole file – George Apr 23 '11 at 19:33

2 Answers 2

If you want a stable sort (relative order of the input rows is preserved in case of ties), you need to use the -s or --stable flag.

share|improve this answer

If you want GNU sort to be stable then you must pass -s. Otherwise, records with the same key will come out in an arbitrary order based on sort's internal algorithm.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.