Sort unix alphabetically then numerically, not working as I intended

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












2















Sorry if this is a duplicate question, but I could not find the answer that I am looking for here or in the documentation.



I have a file that looks like the following:



chr2_oligo1234 700 750
chr2_oligo1236 750 800
chr1_oligo1 50 100
chr1_oligo256 150 200
chr1_oligo6 3500 3550
chr4_oligo95 50 100
chr5_oligo1 50 100
chr4_oligo4 150 200


The desired output looks like:



chr1_oligo1 50 100
chr1_oligo256 150 200
chr1_oligo6 3500 3550
chr2_oligo1234 700 750
chr2_oligo1236 750 800
chr4_oligo95 50 100
chr4_oligo4 150 200
chr5_oligo1 50 100


The pattern at the start (e.g. chr#_oligo#) only matters in terms of the chr#, meaning that all chr1 should be first, then chr2, then chr3, etc., but I would like to sort those substrings numerically in groups as shown by the desired output above. So, I would like to know how to sort alphabetically in the case of the first column, and then keeping that order (chr1->chrN), sort each chunk of data numerically.



I apologize if my wording is not the best for this issue or if it is a duplicate. Trying



sort -k1,1 -nk2


does properly sort numerically, but does not keep the first sort intact (jumbles up the first column and places together all lines with columns 2 and 3 being like:



50 100


I am using Mac OS X.



EDIT: I want changed some of the examples in the first column to show more of what I'm looking for. gsort -V worked great if the name in the first column is in numerical order, but in my data set, it isn't always the case.



I would like to essentially sort each subgroup (in this case, chr1, chr2, etc) by column 2 iteratively. I realize this can be easily done by doing a grep for each and then sorting it on column 2, but I would like to know if sort or another unix command could accomplish this alone.










share|improve this question



















  • 1





    sort -V -k1,1 -k2 file with gsort (part of coreutils)

    – don_crissti
    Sep 29 '16 at 20:47











  • See unix.stackexchange.com/a/289487/117549

    – Jeff Schaller
    Sep 29 '16 at 21:17











  • @JeffSchaller not what I was looking for. Moving the n around does not give the input I would like. Thank you though.

    – implication
    Sep 30 '16 at 1:03











  • @don_crissti I ran "brew install coreutils" on my terminal, and had to install Xcode. This works great! Is there a way to call gsort without typing it? I read that the sort command should be changed to call gsort instead but that does not work for me (invalid option error, meaning it is still calling the OS X sort and not coreutils version.) Thank you! I am changing the question a bit because it seems -V only works if the first column is in a numerical order. This is not always the case. I will change around my question.

    – implication
    Sep 30 '16 at 1:17
















2















Sorry if this is a duplicate question, but I could not find the answer that I am looking for here or in the documentation.



I have a file that looks like the following:



chr2_oligo1234 700 750
chr2_oligo1236 750 800
chr1_oligo1 50 100
chr1_oligo256 150 200
chr1_oligo6 3500 3550
chr4_oligo95 50 100
chr5_oligo1 50 100
chr4_oligo4 150 200


The desired output looks like:



chr1_oligo1 50 100
chr1_oligo256 150 200
chr1_oligo6 3500 3550
chr2_oligo1234 700 750
chr2_oligo1236 750 800
chr4_oligo95 50 100
chr4_oligo4 150 200
chr5_oligo1 50 100


The pattern at the start (e.g. chr#_oligo#) only matters in terms of the chr#, meaning that all chr1 should be first, then chr2, then chr3, etc., but I would like to sort those substrings numerically in groups as shown by the desired output above. So, I would like to know how to sort alphabetically in the case of the first column, and then keeping that order (chr1->chrN), sort each chunk of data numerically.



I apologize if my wording is not the best for this issue or if it is a duplicate. Trying



sort -k1,1 -nk2


does properly sort numerically, but does not keep the first sort intact (jumbles up the first column and places together all lines with columns 2 and 3 being like:



50 100


I am using Mac OS X.



EDIT: I want changed some of the examples in the first column to show more of what I'm looking for. gsort -V worked great if the name in the first column is in numerical order, but in my data set, it isn't always the case.



I would like to essentially sort each subgroup (in this case, chr1, chr2, etc) by column 2 iteratively. I realize this can be easily done by doing a grep for each and then sorting it on column 2, but I would like to know if sort or another unix command could accomplish this alone.










share|improve this question



















  • 1





    sort -V -k1,1 -k2 file with gsort (part of coreutils)

    – don_crissti
    Sep 29 '16 at 20:47











  • See unix.stackexchange.com/a/289487/117549

    – Jeff Schaller
    Sep 29 '16 at 21:17











  • @JeffSchaller not what I was looking for. Moving the n around does not give the input I would like. Thank you though.

    – implication
    Sep 30 '16 at 1:03











  • @don_crissti I ran "brew install coreutils" on my terminal, and had to install Xcode. This works great! Is there a way to call gsort without typing it? I read that the sort command should be changed to call gsort instead but that does not work for me (invalid option error, meaning it is still calling the OS X sort and not coreutils version.) Thank you! I am changing the question a bit because it seems -V only works if the first column is in a numerical order. This is not always the case. I will change around my question.

    – implication
    Sep 30 '16 at 1:17














2












2








2


0






Sorry if this is a duplicate question, but I could not find the answer that I am looking for here or in the documentation.



I have a file that looks like the following:



chr2_oligo1234 700 750
chr2_oligo1236 750 800
chr1_oligo1 50 100
chr1_oligo256 150 200
chr1_oligo6 3500 3550
chr4_oligo95 50 100
chr5_oligo1 50 100
chr4_oligo4 150 200


The desired output looks like:



chr1_oligo1 50 100
chr1_oligo256 150 200
chr1_oligo6 3500 3550
chr2_oligo1234 700 750
chr2_oligo1236 750 800
chr4_oligo95 50 100
chr4_oligo4 150 200
chr5_oligo1 50 100


The pattern at the start (e.g. chr#_oligo#) only matters in terms of the chr#, meaning that all chr1 should be first, then chr2, then chr3, etc., but I would like to sort those substrings numerically in groups as shown by the desired output above. So, I would like to know how to sort alphabetically in the case of the first column, and then keeping that order (chr1->chrN), sort each chunk of data numerically.



I apologize if my wording is not the best for this issue or if it is a duplicate. Trying



sort -k1,1 -nk2


does properly sort numerically, but does not keep the first sort intact (jumbles up the first column and places together all lines with columns 2 and 3 being like:



50 100


I am using Mac OS X.



EDIT: I want changed some of the examples in the first column to show more of what I'm looking for. gsort -V worked great if the name in the first column is in numerical order, but in my data set, it isn't always the case.



I would like to essentially sort each subgroup (in this case, chr1, chr2, etc) by column 2 iteratively. I realize this can be easily done by doing a grep for each and then sorting it on column 2, but I would like to know if sort or another unix command could accomplish this alone.










share|improve this question
















Sorry if this is a duplicate question, but I could not find the answer that I am looking for here or in the documentation.



I have a file that looks like the following:



chr2_oligo1234 700 750
chr2_oligo1236 750 800
chr1_oligo1 50 100
chr1_oligo256 150 200
chr1_oligo6 3500 3550
chr4_oligo95 50 100
chr5_oligo1 50 100
chr4_oligo4 150 200


The desired output looks like:



chr1_oligo1 50 100
chr1_oligo256 150 200
chr1_oligo6 3500 3550
chr2_oligo1234 700 750
chr2_oligo1236 750 800
chr4_oligo95 50 100
chr4_oligo4 150 200
chr5_oligo1 50 100


The pattern at the start (e.g. chr#_oligo#) only matters in terms of the chr#, meaning that all chr1 should be first, then chr2, then chr3, etc., but I would like to sort those substrings numerically in groups as shown by the desired output above. So, I would like to know how to sort alphabetically in the case of the first column, and then keeping that order (chr1->chrN), sort each chunk of data numerically.



I apologize if my wording is not the best for this issue or if it is a duplicate. Trying



sort -k1,1 -nk2


does properly sort numerically, but does not keep the first sort intact (jumbles up the first column and places together all lines with columns 2 and 3 being like:



50 100


I am using Mac OS X.



EDIT: I want changed some of the examples in the first column to show more of what I'm looking for. gsort -V worked great if the name in the first column is in numerical order, but in my data set, it isn't always the case.



I would like to essentially sort each subgroup (in this case, chr1, chr2, etc) by column 2 iteratively. I realize this can be easily done by doing a grep for each and then sorting it on column 2, but I would like to know if sort or another unix command could accomplish this alone.







text-processing sort






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 1 '17 at 6:25









BitsOfNix

4,14821832




4,14821832










asked Sep 29 '16 at 20:37









implicationimplication

113




113







  • 1





    sort -V -k1,1 -k2 file with gsort (part of coreutils)

    – don_crissti
    Sep 29 '16 at 20:47











  • See unix.stackexchange.com/a/289487/117549

    – Jeff Schaller
    Sep 29 '16 at 21:17











  • @JeffSchaller not what I was looking for. Moving the n around does not give the input I would like. Thank you though.

    – implication
    Sep 30 '16 at 1:03











  • @don_crissti I ran "brew install coreutils" on my terminal, and had to install Xcode. This works great! Is there a way to call gsort without typing it? I read that the sort command should be changed to call gsort instead but that does not work for me (invalid option error, meaning it is still calling the OS X sort and not coreutils version.) Thank you! I am changing the question a bit because it seems -V only works if the first column is in a numerical order. This is not always the case. I will change around my question.

    – implication
    Sep 30 '16 at 1:17













  • 1





    sort -V -k1,1 -k2 file with gsort (part of coreutils)

    – don_crissti
    Sep 29 '16 at 20:47











  • See unix.stackexchange.com/a/289487/117549

    – Jeff Schaller
    Sep 29 '16 at 21:17











  • @JeffSchaller not what I was looking for. Moving the n around does not give the input I would like. Thank you though.

    – implication
    Sep 30 '16 at 1:03











  • @don_crissti I ran "brew install coreutils" on my terminal, and had to install Xcode. This works great! Is there a way to call gsort without typing it? I read that the sort command should be changed to call gsort instead but that does not work for me (invalid option error, meaning it is still calling the OS X sort and not coreutils version.) Thank you! I am changing the question a bit because it seems -V only works if the first column is in a numerical order. This is not always the case. I will change around my question.

    – implication
    Sep 30 '16 at 1:17








1




1





sort -V -k1,1 -k2 file with gsort (part of coreutils)

– don_crissti
Sep 29 '16 at 20:47





sort -V -k1,1 -k2 file with gsort (part of coreutils)

– don_crissti
Sep 29 '16 at 20:47













See unix.stackexchange.com/a/289487/117549

– Jeff Schaller
Sep 29 '16 at 21:17





See unix.stackexchange.com/a/289487/117549

– Jeff Schaller
Sep 29 '16 at 21:17













@JeffSchaller not what I was looking for. Moving the n around does not give the input I would like. Thank you though.

– implication
Sep 30 '16 at 1:03





@JeffSchaller not what I was looking for. Moving the n around does not give the input I would like. Thank you though.

– implication
Sep 30 '16 at 1:03













@don_crissti I ran "brew install coreutils" on my terminal, and had to install Xcode. This works great! Is there a way to call gsort without typing it? I read that the sort command should be changed to call gsort instead but that does not work for me (invalid option error, meaning it is still calling the OS X sort and not coreutils version.) Thank you! I am changing the question a bit because it seems -V only works if the first column is in a numerical order. This is not always the case. I will change around my question.

– implication
Sep 30 '16 at 1:17






@don_crissti I ran "brew install coreutils" on my terminal, and had to install Xcode. This works great! Is there a way to call gsort without typing it? I read that the sort command should be changed to call gsort instead but that does not work for me (invalid option error, meaning it is still calling the OS X sort and not coreutils version.) Thank you! I am changing the question a bit because it seems -V only works if the first column is in a numerical order. This is not always the case. I will change around my question.

– implication
Sep 30 '16 at 1:17











2 Answers
2






active

oldest

votes


















0














sort -k1,1 -nk2 is the same as sort -k1,1 -n -k2, same as sort -n -k1,1 -k2, as in the numerical sorting is turned on globally, for all the keys.



To sort the 2nd key only numerically, you need to add n to that sort key description as in:



sort -k1,1 -k2n


Or:



sort -k1,1 -k2,2n


With n and with the default field separator 2 is the same as 2,2 though. 2 would be the part of the line starting from the second field, but when interpreted as a number, that's the same as the second field alone (2,2).



Here, you could also sort numerically on the number that is after chr and then alphabetically on the rest of the first field and then numerically on the second field with:



sort -k1.4n -k1,1 -k2n





share|improve this answer
































    0














    sorting alphabetically on the 1st field, and numerically on the 2nd gives (in your output, -- chr4_oligo95 is before ch4_oligo4)



    sort -k1,1 -k2n,2n file 
    chr1_oligo1 50 100
    chr1_oligo256 150 200
    chr1_oligo6 3500 3550
    chr2_oligo1234 700 750
    chr2_oligo1236 750 800
    chr4_oligo4 150 200
    chr4_oligo95 50 100
    chr5_oligo1 50 100





    share|improve this answer






















      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%2f313280%2fsort-unix-alphabetically-then-numerically-not-working-as-i-intended%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      sort -k1,1 -nk2 is the same as sort -k1,1 -n -k2, same as sort -n -k1,1 -k2, as in the numerical sorting is turned on globally, for all the keys.



      To sort the 2nd key only numerically, you need to add n to that sort key description as in:



      sort -k1,1 -k2n


      Or:



      sort -k1,1 -k2,2n


      With n and with the default field separator 2 is the same as 2,2 though. 2 would be the part of the line starting from the second field, but when interpreted as a number, that's the same as the second field alone (2,2).



      Here, you could also sort numerically on the number that is after chr and then alphabetically on the rest of the first field and then numerically on the second field with:



      sort -k1.4n -k1,1 -k2n





      share|improve this answer





























        0














        sort -k1,1 -nk2 is the same as sort -k1,1 -n -k2, same as sort -n -k1,1 -k2, as in the numerical sorting is turned on globally, for all the keys.



        To sort the 2nd key only numerically, you need to add n to that sort key description as in:



        sort -k1,1 -k2n


        Or:



        sort -k1,1 -k2,2n


        With n and with the default field separator 2 is the same as 2,2 though. 2 would be the part of the line starting from the second field, but when interpreted as a number, that's the same as the second field alone (2,2).



        Here, you could also sort numerically on the number that is after chr and then alphabetically on the rest of the first field and then numerically on the second field with:



        sort -k1.4n -k1,1 -k2n





        share|improve this answer



























          0












          0








          0







          sort -k1,1 -nk2 is the same as sort -k1,1 -n -k2, same as sort -n -k1,1 -k2, as in the numerical sorting is turned on globally, for all the keys.



          To sort the 2nd key only numerically, you need to add n to that sort key description as in:



          sort -k1,1 -k2n


          Or:



          sort -k1,1 -k2,2n


          With n and with the default field separator 2 is the same as 2,2 though. 2 would be the part of the line starting from the second field, but when interpreted as a number, that's the same as the second field alone (2,2).



          Here, you could also sort numerically on the number that is after chr and then alphabetically on the rest of the first field and then numerically on the second field with:



          sort -k1.4n -k1,1 -k2n





          share|improve this answer















          sort -k1,1 -nk2 is the same as sort -k1,1 -n -k2, same as sort -n -k1,1 -k2, as in the numerical sorting is turned on globally, for all the keys.



          To sort the 2nd key only numerically, you need to add n to that sort key description as in:



          sort -k1,1 -k2n


          Or:



          sort -k1,1 -k2,2n


          With n and with the default field separator 2 is the same as 2,2 though. 2 would be the part of the line starting from the second field, but when interpreted as a number, that's the same as the second field alone (2,2).



          Here, you could also sort numerically on the number that is after chr and then alphabetically on the rest of the first field and then numerically on the second field with:



          sort -k1.4n -k1,1 -k2n






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 30 '16 at 7:50

























          answered Sep 30 '16 at 7:24









          Stéphane ChazelasStéphane Chazelas

          302k56568922




          302k56568922























              0














              sorting alphabetically on the 1st field, and numerically on the 2nd gives (in your output, -- chr4_oligo95 is before ch4_oligo4)



              sort -k1,1 -k2n,2n file 
              chr1_oligo1 50 100
              chr1_oligo256 150 200
              chr1_oligo6 3500 3550
              chr2_oligo1234 700 750
              chr2_oligo1236 750 800
              chr4_oligo4 150 200
              chr4_oligo95 50 100
              chr5_oligo1 50 100





              share|improve this answer



























                0














                sorting alphabetically on the 1st field, and numerically on the 2nd gives (in your output, -- chr4_oligo95 is before ch4_oligo4)



                sort -k1,1 -k2n,2n file 
                chr1_oligo1 50 100
                chr1_oligo256 150 200
                chr1_oligo6 3500 3550
                chr2_oligo1234 700 750
                chr2_oligo1236 750 800
                chr4_oligo4 150 200
                chr4_oligo95 50 100
                chr5_oligo1 50 100





                share|improve this answer

























                  0












                  0








                  0







                  sorting alphabetically on the 1st field, and numerically on the 2nd gives (in your output, -- chr4_oligo95 is before ch4_oligo4)



                  sort -k1,1 -k2n,2n file 
                  chr1_oligo1 50 100
                  chr1_oligo256 150 200
                  chr1_oligo6 3500 3550
                  chr2_oligo1234 700 750
                  chr2_oligo1236 750 800
                  chr4_oligo4 150 200
                  chr4_oligo95 50 100
                  chr5_oligo1 50 100





                  share|improve this answer













                  sorting alphabetically on the 1st field, and numerically on the 2nd gives (in your output, -- chr4_oligo95 is before ch4_oligo4)



                  sort -k1,1 -k2n,2n file 
                  chr1_oligo1 50 100
                  chr1_oligo256 150 200
                  chr1_oligo6 3500 3550
                  chr2_oligo1234 700 750
                  chr2_oligo1236 750 800
                  chr4_oligo4 150 200
                  chr4_oligo95 50 100
                  chr5_oligo1 50 100






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 30 '16 at 13:21









                  jai_sjai_s

                  1,31036




                  1,31036



























                      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%2f313280%2fsort-unix-alphabetically-then-numerically-not-working-as-i-intended%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?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay