The two examples in the title produce very different results. The first (sort -k2,2 -nk6,6 foo
) brings back the following results:
153759 townhome 900 2 1 79000
876543 townhome 745 2 1 79000
222999 townhome 850 2 2 83333
759153 condo 850 2 1.5 85000
453215 townhome 1000 3 1.5 86000
646484 condo 890 3 1 93333
444555 condo 930 2 1 99999
etc.
The second (sort -k2,2 -k6,6n foo
) brings back:
759153 condo 850 2 1.5 85000
646484 condo 890 3 1 93333
444555 condo 930 2 1 99999
777894 condo 790 3 1 101000
221155 condo 1030 3 1 109500
248624 duplex 1250 3 1 120000
987654 duplex 1100 3 1.5 140000
These are the correct results I'm looking for.
What causes the difference?
Best Answer
As mikeserv said, but with more words:
Using the 'global'
-n
numeric sort option changes sort's global behavior to use numeric sorting for all the keys (reference) by setting gkey.numeric and then later setting every key's sort option to the globally-set numeric-sort option. This causes the sorting on field 2 to fail, so it falls through to sorting (numerically) on field 6.Setting the
n
ordering option inside the-k
option only affects sorting on that field, so it can then sort on field 2 until that's not unique, then fall through to numerically sorting on field 6.Because I could not find 'duplex' lines in your first sort example, I uniquely combined all of the sample lines to create a new sample input file:
...against which we can demonstrate sort's behavior with the
--debug
flag; notice the "no match" warnings and the underscores on the 6th field, indicating the portion of the line that sort used for sorting:vs: