Sort based on field1, field2 but preserving field 1 sort and ignoring nulls/blanks in 2nd sort

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5















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









share|improve this question
























  • Hi but I didnt get your scenario .

    – rɑːdʒɑ
    Mar 25 '15 at 5:58

















5















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









share|improve this question
























  • Hi but I didnt get your scenario .

    – rɑːdʒɑ
    Mar 25 '15 at 5:58













5












5








5








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









share|improve this question
















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






text-processing sort






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 10 at 4:23









Rui F Ribeiro

42k1483142




42k1483142










asked Mar 25 '15 at 3:51









Suresh NarasimhaSuresh Narasimha

284




284












  • Hi but I didnt get your scenario .

    – rɑːdʒɑ
    Mar 25 '15 at 5:58

















  • Hi but I didnt get your scenario .

    – rɑːdʒɑ
    Mar 25 '15 at 5:58
















Hi but I didnt get your scenario .

– rɑːdʒɑ
Mar 25 '15 at 5:58





Hi but I didnt get your scenario .

– rɑːdʒɑ
Mar 25 '15 at 5:58










1 Answer
1






active

oldest

votes


















6














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





share|improve this answer























  • Brilliant !! may be a simple answer for you, but I did this sort years back without knowing the real application and usage. I now realise the full potential of sort command Thanks very much for your quick response and help!!

    – Suresh Narasimha
    Mar 25 '15 at 11:29












  • @SureshNarasimha If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.

    – terdon
    Mar 25 '15 at 13:12











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f192353%2fsort-based-on-field1-field2-but-preserving-field-1-sort-and-ignoring-nulls-blan%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














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





share|improve this answer























  • Brilliant !! may be a simple answer for you, but I did this sort years back without knowing the real application and usage. I now realise the full potential of sort command Thanks very much for your quick response and help!!

    – Suresh Narasimha
    Mar 25 '15 at 11:29












  • @SureshNarasimha If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.

    – terdon
    Mar 25 '15 at 13:12















6














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





share|improve this answer























  • Brilliant !! may be a simple answer for you, but I did this sort years back without knowing the real application and usage. I now realise the full potential of sort command Thanks very much for your quick response and help!!

    – Suresh Narasimha
    Mar 25 '15 at 11:29












  • @SureshNarasimha If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.

    – terdon
    Mar 25 '15 at 13:12













6












6








6







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





share|improve this 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






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 25 '15 at 6:51









Stéphane ChazelasStéphane Chazelas

313k57593949




313k57593949












  • Brilliant !! may be a simple answer for you, but I did this sort years back without knowing the real application and usage. I now realise the full potential of sort command Thanks very much for your quick response and help!!

    – Suresh Narasimha
    Mar 25 '15 at 11:29












  • @SureshNarasimha If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.

    – terdon
    Mar 25 '15 at 13:12

















  • Brilliant !! may be a simple answer for you, but I did this sort years back without knowing the real application and usage. I now realise the full potential of sort command Thanks very much for your quick response and help!!

    – Suresh Narasimha
    Mar 25 '15 at 11:29












  • @SureshNarasimha If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.

    – terdon
    Mar 25 '15 at 13:12
















Brilliant !! may be a simple answer for you, but I did this sort years back without knowing the real application and usage. I now realise the full potential of sort command Thanks very much for your quick response and help!!

– Suresh Narasimha
Mar 25 '15 at 11:29






Brilliant !! may be a simple answer for you, but I did this sort years back without knowing the real application and usage. I now realise the full potential of sort command Thanks very much for your quick response and help!!

– Suresh Narasimha
Mar 25 '15 at 11:29














@SureshNarasimha If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.

– terdon
Mar 25 '15 at 13:12





@SureshNarasimha If this answer solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.

– terdon
Mar 25 '15 at 13:12

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f192353%2fsort-based-on-field1-field2-but-preserving-field-1-sort-and-ignoring-nulls-blan%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?