5

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"

3
  • kalispera file :) Goodys gia panta :P
    – SpyrosP
    Apr 23, 2011 at 19:16
  • 1
    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, 2011 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, 2011 at 19:33

2 Answers 2

5

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.

0
4

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.

0

You must log in to answer this question.