Sorting file by first and then second column

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












13















How can I manipulate a two columns tab-separated text file by sorting by the first element of the second column (only if the first column element is the same)?



Exemple:



Input File 1



A 1-2
A 6-8
A 3-4
B 7-10
B 5-9


Expected output: File 2



A 1-2
A 3-4
A 6-8
B 5-9
B 7-10









share|improve this question




























    13















    How can I manipulate a two columns tab-separated text file by sorting by the first element of the second column (only if the first column element is the same)?



    Exemple:



    Input File 1



    A 1-2
    A 6-8
    A 3-4
    B 7-10
    B 5-9


    Expected output: File 2



    A 1-2
    A 3-4
    A 6-8
    B 5-9
    B 7-10









    share|improve this question


























      13












      13








      13


      1






      How can I manipulate a two columns tab-separated text file by sorting by the first element of the second column (only if the first column element is the same)?



      Exemple:



      Input File 1



      A 1-2
      A 6-8
      A 3-4
      B 7-10
      B 5-9


      Expected output: File 2



      A 1-2
      A 3-4
      A 6-8
      B 5-9
      B 7-10









      share|improve this question
















      How can I manipulate a two columns tab-separated text file by sorting by the first element of the second column (only if the first column element is the same)?



      Exemple:



      Input File 1



      A 1-2
      A 6-8
      A 3-4
      B 7-10
      B 5-9


      Expected output: File 2



      A 1-2
      A 3-4
      A 6-8
      B 5-9
      B 7-10






      awk sort






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 1 at 14:15









      user2740

      1034




      1034










      asked Jul 16 '14 at 7:12









      dovahdovah

      80751329




      80751329




















          1 Answer
          1






          active

          oldest

          votes


















          22














          Use sort's -k option to sort by (multiple) columns at once:



          $ sort -k1,1 -k2n input
          A 1-2
          A 3-4
          A 6-8
          B 5-9
          B 7-10


          -k1,1 sorts by the first column first, then -k2n by the second¹ numerically when the first column was tied, so you get your output in the order you want: sorting by the first element of the second column, only if the first column element is the same.



          When sorting numerically it will only examine the field until it stops being a number, so that gives you a comparison of just the first element of it.



          When the two keys compare the same, then sort compares the full lines lexically as a last resort comparison. For instance in A 1-10 vs A 1-2, the first keys are identical (A string), and the second key as well (both are treated as the number 1), so then sort compares A 1-10 vs A 1-2 lexically and the latter is greater as 2 sorts after 1. The GNU implementation of sort has a -V option or V key flag to perform a version sort, which is like a lexical comparison except that sequences of decimal digits within the strings are compared numerically, so sort -k1,1 -k2V would sort A 1-10 after A 1-2 because 10 as a number is greater than 2.




          ¹ technically, -k2 means the portion of the line starting with the second field (after the first transition from a non-blank to a blank) and ending at the end of the line, but with the n flag, that's equivalent to -k2,2n as only the leading part that constitutes a number is considered.






          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%2f144758%2fsorting-file-by-first-and-then-second-column%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









            22














            Use sort's -k option to sort by (multiple) columns at once:



            $ sort -k1,1 -k2n input
            A 1-2
            A 3-4
            A 6-8
            B 5-9
            B 7-10


            -k1,1 sorts by the first column first, then -k2n by the second¹ numerically when the first column was tied, so you get your output in the order you want: sorting by the first element of the second column, only if the first column element is the same.



            When sorting numerically it will only examine the field until it stops being a number, so that gives you a comparison of just the first element of it.



            When the two keys compare the same, then sort compares the full lines lexically as a last resort comparison. For instance in A 1-10 vs A 1-2, the first keys are identical (A string), and the second key as well (both are treated as the number 1), so then sort compares A 1-10 vs A 1-2 lexically and the latter is greater as 2 sorts after 1. The GNU implementation of sort has a -V option or V key flag to perform a version sort, which is like a lexical comparison except that sequences of decimal digits within the strings are compared numerically, so sort -k1,1 -k2V would sort A 1-10 after A 1-2 because 10 as a number is greater than 2.




            ¹ technically, -k2 means the portion of the line starting with the second field (after the first transition from a non-blank to a blank) and ending at the end of the line, but with the n flag, that's equivalent to -k2,2n as only the leading part that constitutes a number is considered.






            share|improve this answer





























              22














              Use sort's -k option to sort by (multiple) columns at once:



              $ sort -k1,1 -k2n input
              A 1-2
              A 3-4
              A 6-8
              B 5-9
              B 7-10


              -k1,1 sorts by the first column first, then -k2n by the second¹ numerically when the first column was tied, so you get your output in the order you want: sorting by the first element of the second column, only if the first column element is the same.



              When sorting numerically it will only examine the field until it stops being a number, so that gives you a comparison of just the first element of it.



              When the two keys compare the same, then sort compares the full lines lexically as a last resort comparison. For instance in A 1-10 vs A 1-2, the first keys are identical (A string), and the second key as well (both are treated as the number 1), so then sort compares A 1-10 vs A 1-2 lexically and the latter is greater as 2 sorts after 1. The GNU implementation of sort has a -V option or V key flag to perform a version sort, which is like a lexical comparison except that sequences of decimal digits within the strings are compared numerically, so sort -k1,1 -k2V would sort A 1-10 after A 1-2 because 10 as a number is greater than 2.




              ¹ technically, -k2 means the portion of the line starting with the second field (after the first transition from a non-blank to a blank) and ending at the end of the line, but with the n flag, that's equivalent to -k2,2n as only the leading part that constitutes a number is considered.






              share|improve this answer



























                22












                22








                22







                Use sort's -k option to sort by (multiple) columns at once:



                $ sort -k1,1 -k2n input
                A 1-2
                A 3-4
                A 6-8
                B 5-9
                B 7-10


                -k1,1 sorts by the first column first, then -k2n by the second¹ numerically when the first column was tied, so you get your output in the order you want: sorting by the first element of the second column, only if the first column element is the same.



                When sorting numerically it will only examine the field until it stops being a number, so that gives you a comparison of just the first element of it.



                When the two keys compare the same, then sort compares the full lines lexically as a last resort comparison. For instance in A 1-10 vs A 1-2, the first keys are identical (A string), and the second key as well (both are treated as the number 1), so then sort compares A 1-10 vs A 1-2 lexically and the latter is greater as 2 sorts after 1. The GNU implementation of sort has a -V option or V key flag to perform a version sort, which is like a lexical comparison except that sequences of decimal digits within the strings are compared numerically, so sort -k1,1 -k2V would sort A 1-10 after A 1-2 because 10 as a number is greater than 2.




                ¹ technically, -k2 means the portion of the line starting with the second field (after the first transition from a non-blank to a blank) and ending at the end of the line, but with the n flag, that's equivalent to -k2,2n as only the leading part that constitutes a number is considered.






                share|improve this answer















                Use sort's -k option to sort by (multiple) columns at once:



                $ sort -k1,1 -k2n input
                A 1-2
                A 3-4
                A 6-8
                B 5-9
                B 7-10


                -k1,1 sorts by the first column first, then -k2n by the second¹ numerically when the first column was tied, so you get your output in the order you want: sorting by the first element of the second column, only if the first column element is the same.



                When sorting numerically it will only examine the field until it stops being a number, so that gives you a comparison of just the first element of it.



                When the two keys compare the same, then sort compares the full lines lexically as a last resort comparison. For instance in A 1-10 vs A 1-2, the first keys are identical (A string), and the second key as well (both are treated as the number 1), so then sort compares A 1-10 vs A 1-2 lexically and the latter is greater as 2 sorts after 1. The GNU implementation of sort has a -V option or V key flag to perform a version sort, which is like a lexical comparison except that sequences of decimal digits within the strings are compared numerically, so sort -k1,1 -k2V would sort A 1-10 after A 1-2 because 10 as a number is greater than 2.




                ¹ technically, -k2 means the portion of the line starting with the second field (after the first transition from a non-blank to a blank) and ending at the end of the line, but with the n flag, that's equivalent to -k2,2n as only the leading part that constitutes a number is considered.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 1 at 15:05









                Stéphane Chazelas

                312k57589946




                312k57589946










                answered Jul 16 '14 at 7:31









                Michael HomerMichael Homer

                50.4k8139176




                50.4k8139176



























                    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%2f144758%2fsorting-file-by-first-and-then-second-column%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?