2

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?

1
  • the first one sorts both numerically, the second only sorts the second numerically.
    – mikeserv
    Nov 29, 2015 at 20:31

1 Answer 1

1

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:

153759 townhome 900 2 1 79000
221155 condo 1030 3 1 109500
222999 townhome 850 2 2 83333
248624 duplex 1250 3 1 120000
444555 condo 930 2 1 99999
453215 townhome 1000 3 1.5 86000
646484 condo 890 3 1 93333
759153 condo 850 2 1.5 85000
777894 condo 790 3 1 101000
876543 townhome 745 2 1 79000
987654 duplex 1100 3 1.5 140000

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

$ sort -k2,2 -nk6,6 --debug < input
sort: using ‘en_US.UTF-8’ sorting rules
153759 townhome 900 2 1 79000
       ^ no match for key
                        _____
_____________________________
876543 townhome 745 2 1 79000
       ^ no match for key
                        _____
_____________________________
222999 townhome 850 2 2 83333
       ^ no match for key
                        _____
_____________________________
759153 condo 850 2 1.5 85000
       ^ no match for key
                       _____
____________________________
453215 townhome 1000 3 1.5 86000
       ^ no match for key
                           _____
________________________________
646484 condo 890 3 1 93333
       ^ no match for key
                     _____
__________________________
444555 condo 930 2 1 99999
       ^ no match for key
                     _____
__________________________
777894 condo 790 3 1 101000
       ^ no match for key
                     ______
___________________________
221155 condo 1030 3 1 109500
       ^ no match for key
                      ______
____________________________
248624 duplex 1250 3 1 120000
       ^ no match for key
                       ______
_____________________________
987654 duplex 1100 3 1.5 140000
       ^ no match for key
                         ______
_______________________________

vs:

$ sort -k2,2 -k6,6n --debug < input
sort: using ‘en_US.UTF-8’ sorting rules
sort: leading blanks are significant in key 1; consider also specifying 'b'
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
      _______
                         ______
_______________________________
153759 townhome 900 2 1 79000
      _________
                        _____
_____________________________
876543 townhome 745 2 1 79000
      _________
                        _____
_____________________________
222999 townhome 850 2 2 83333
      _________
                        _____
_____________________________
453215 townhome 1000 3 1.5 86000
      _________
                           _____
________________________________

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.