数値オプションを使用したときのソート動作:「sort -k2,2 -nk6,6 foo」と「sort -k2,2 -k6,6n foo」

数値オプションを使用したときのソート動作:「sort -k2,2 -nk6,6 foo」と「sort -k2,2 -k6,6n foo」

タイトルの2つの例は非常に異なる結果をもたらします。最初の項目(sort -k2,2 -nk6,6 foo)は次の結果を返します。

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

など。

2番目(sort -k2,2 -k6,6n foo)は次を返します。

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

これが私が探している正しい結果です。

この違いの原因は何ですか?

ベストアンサー1

〜のようにマイクセルフしかし、より多くの単語は次のとおりです。

「グローバル」-n数値ソートオプションを使用すると、すべてのキーに対して数値ソートを使用するようにソートのグローバル動作が変更されます(引用する)gkey.numericを設定してから設定する各キーの設定グローバルに設定された数値ソートオプションのソートオプション。これによりフィールド2のソートが失敗するため、代わりにフィールド6がソート(数値)されます。

設定n 注文オプション内部的には、このオプションは-kこのフィールドの並べ替えにのみ影響するため、フィールド2が一意でないまで並べ替えてから、フィールド6を数値で並べ替えます。

最初のソートされた例では「二重」行が見つからなかったため、すべての例行を一意に組み合わせて新しいサンプル入力ファイルを作成しました。

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

...フラグを使用してソートの動作を表示できます--debug。 「一致しない」警告と並べ替えに使用される行の部分を表す6番目のフィールドの下線を参照してください。

$ 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
                         ______
_______________________________

比較:

$ 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
      _________
                           _____
________________________________

おすすめ記事