Text Processing Sort – Sort Based on Field1 and Field2 Ignoring Nulls in Field2

sorttext processing

Input data after sorting by Column 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

If I force numeric sort on 2nd field, blank values are pushed to top, this disturbs my first field sort

-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

Ideally I was trying to acheive this

"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

Best Answer

You need to specify where sort keys end. Otherwise they end at the end of the line.

And to apply numeric sort to one key only, that's with n appended to the key spec. -n alone would turn numeric sort on globally.

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