基于字段 1、字段 2 进行排序,但保留字段 1 排序并忽略第二个排序中的空值/空白

基于字段 1、字段 2 进行排序,但保留字段 1 排序并忽略第二个排序中的空值/空白

按第 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

如果我强制对第二个字段进行数字排序,空白值将被推到顶部,这会干扰我的第一个字段排序

-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

您需要指定排序键的结束位置。否则它们会在行尾结束。

如果要将数字排序仅应用于一个键,则将其n附加到键规范中。-n单独将在全局范围内打开数字排序。

sort -t'|' -k1,1 -k2,2n

相关内容