1列目でソート後の入力
-bash-3.2$ sort -t'|' -k1 -k2 clm.srt
"1033CLMS0400000000000"||""|"0000000558 "| |1
"2020000005438WC011631"||""|" 200606202"| |2
"2020000005438WC011632"|10|"N"|"20060626N00000200000"|10 |3
"2020000005438WC011632"|11|"N"|"20060626N00000200000"|11 |4
"2020000005438WC011632"|12|"N"|"20060626N00000200000"|12 |5
"2020000005438WC011632"|1|"N"|"20060620N000020000000"|1 |13
"2020000005438WC011632"|47|"N"|"20060626N00000000000"|47 |43
"2020000005438WC011632"|5|"N"|"20060626N000000000000"|5 |45
"2020000005438WC011632"|6|"N"|"20060626N0002N0200000"|6 |46
"2020000005438WC011632"|7|"N"|"20060626N000002N00000"|7 |47
"2020000005438WC011632"|8|"N"|"20060626N000000200000"|8 |48
"2020000005438WC011632"|9|"N"|"20060626N0000N0200000"|9 |49
"2020000005438WC011633"||""|"20060605000 "| |50
"2020000005438WC011634"||""|"001033720061 "| |51
"2020000005438WC011635"||""|"0020060626N+00014000000"| |52
2番目のフィールドに数値の並べ替えを強制すると、空白の値が一番上に押され、最初のフィールドの並べ替えが妨げられます。
-bash-3.2$ sort -t'|' -k1 -n -k2 clm.srt
"1033CLMS0400000000000"||""|"0000000558 "| |1
"2020000005438WC011631"||""|" 200606202"| |2
"2020000005438WC011633"||""|"20060605000 "| |50
"2020000005438WC011634"||""|"001033720061 "| |51
"2020000005438WC011635"||""|"0020060626N+00014000000"| |52
"2020000005438WC011632"|1|"N"|"20060620N000020000000"|1 |13
"2020000005438WC011632"|5|"N"|"20060626N000000000000"|5 |45
"2020000005438WC011632"|6|"N"|"20060626N0002N0200000"|6 |46
"2020000005438WC011632"|7|"N"|"20060626N000002N00000"|7 |47
"2020000005438WC011632"|8|"N"|"20060626N000000200000"|8 |48
"2020000005438WC011632"|9|"N"|"20060626N0000N0200000"|9 |49
"2020000005438WC011632"|10|"N"|"20060626N00000200000"|10 |3
"2020000005438WC011632"|11|"N"|"20060626N00000200000"|11 |4
"2020000005438WC011632"|12|"N"|"20060626N00000200000"|12 |5
"2020000005438WC011632"|47|"N"|"20060626N00000000000"|47 |43
理想的には、これを達成しようとしています。
"1033CLMS0400000000000"||""|"0000000558 "| |1
"2020000005438WC011631"||""|" 200606202"| |2
"2020000005438WC011632"|1|"N"|"20060620N000020000000"|1 |13
"2020000005438WC011632"|5|"N"|"20060626N000000000000"|5 |45
"2020000005438WC011632"|6|"N"|"20060626N0002N0200000"|6 |46
"2020000005438WC011632"|7|"N"|"20060626N000002N00000"|7 |47
"2020000005438WC011632"|8|"N"|"20060626N000000200000"|8 |48
"2020000005438WC011632"|9|"N"|"20060626N0000N0200000"|9 |49
"2020000005438WC011632"|10|"N"|"20060626N00000200000"|10 |3
"2020000005438WC011632"|11|"N"|"20060626N00000200000"|11 |4
"2020000005438WC011632"|12|"N"|"20060626N00000200000"|12 |5
"2020000005438WC011632"|47|"N"|"20060626N00000000000"|47 |43
"2020000005438WC011633"||""|"20060605000 "| |50
"2020000005438WC011634"||""|"001033720061 "| |51
"2020000005438WC011635"||""|"0020060626N+00014000000"| |52
答え1
ソートキーの終了位置を指定する必要があります。それ以外の場合は、行の末尾で終了します。
1つのキーにのみ数値ソートを適用するには、n
それをキー仕様に追加します。-n
Aloneはグローバルに数値ソートをオンにします。
sort -t'|' -k1,1 -k2,2n