How to UNIX sort by one column only?












44















I know that the -k option for the Unix sort allow us to sort by a specific column and all of the following. For instance, given the input file:



2 3
2 2
1 2
2 1
1 1


Using sort -n -k 1, I get an output sorted by the 1st column and then by the 2nd:



1 1
1 2
2 1
2 2
2 3


However, I want to keep the 2nd column ordering, like this:



1 2
1 1
2 3
2 2
2 1


Is this possible with the sort command?










share|improve this question















migrated from stackoverflow.com Sep 1 '09 at 4:49


This question came from our site for professional and enthusiast programmers.























    44















    I know that the -k option for the Unix sort allow us to sort by a specific column and all of the following. For instance, given the input file:



    2 3
    2 2
    1 2
    2 1
    1 1


    Using sort -n -k 1, I get an output sorted by the 1st column and then by the 2nd:



    1 1
    1 2
    2 1
    2 2
    2 3


    However, I want to keep the 2nd column ordering, like this:



    1 2
    1 1
    2 3
    2 2
    2 1


    Is this possible with the sort command?










    share|improve this question















    migrated from stackoverflow.com Sep 1 '09 at 4:49


    This question came from our site for professional and enthusiast programmers.





















      44












      44








      44


      8






      I know that the -k option for the Unix sort allow us to sort by a specific column and all of the following. For instance, given the input file:



      2 3
      2 2
      1 2
      2 1
      1 1


      Using sort -n -k 1, I get an output sorted by the 1st column and then by the 2nd:



      1 1
      1 2
      2 1
      2 2
      2 3


      However, I want to keep the 2nd column ordering, like this:



      1 2
      1 1
      2 3
      2 2
      2 1


      Is this possible with the sort command?










      share|improve this question
















      I know that the -k option for the Unix sort allow us to sort by a specific column and all of the following. For instance, given the input file:



      2 3
      2 2
      1 2
      2 1
      1 1


      Using sort -n -k 1, I get an output sorted by the 1st column and then by the 2nd:



      1 1
      1 2
      2 1
      2 2
      2 3


      However, I want to keep the 2nd column ordering, like this:



      1 2
      1 1
      2 3
      2 2
      2 1


      Is this possible with the sort command?







      unix sorting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 15 '10 at 0:25









      Peter Mortensen

      8,371166185




      8,371166185










      asked Aug 31 '09 at 16:20









      ssnssn

      323136




      323136




      migrated from stackoverflow.com Sep 1 '09 at 4:49


      This question came from our site for professional and enthusiast programmers.









      migrated from stackoverflow.com Sep 1 '09 at 4:49


      This question came from our site for professional and enthusiast programmers.
























          3 Answers
          3






          active

          oldest

          votes


















          60














          Give this a try:



          sort -s -n -k 1,1


          The -s disables 'last-resort' sorting, which sorts on everything that wasn't part of a specified key.



          The -k 1 doesn't actually mean "this field and all of the following" in the context of numeric sort, as you can see if you try to sort on the second column. You're merely seeing ties broken by going to the rest of the line. In general, however, you need to specify -k 1,1 to sort only on field one.






          share|improve this answer


























          • You are right. This is exactly what I needed. Thanks!

            – ssn
            Aug 31 '09 at 16:31











          • is it possible to use join on the output of this sort?

            – MiNdFrEaK
            Sep 27 '12 at 20:13











          • @MiNdFrEaK: The requirement of join is that the input be sorted on the fields you're joining on. So sure, this output is sorted on the first field, and you can join on it.

            – Cascabel
            Sep 27 '12 at 21:49











          • I have 2 files, one having 2 columns, another having 1 column. The second file is sorted using sort -u. Now the task is I need to join this column with the first column of the first file, which is not sorted, so what will be the syntax? will this work? join -j 1 file2.txt sort -s -n -k 1 file1.txt?

            – MiNdFrEaK
            Sep 28 '12 at 4:10






          • 1





            The -k 1,1 (the ",1" part) doesn't work any better for me. What works is -s -k 1, with -n if you need it.

            – Totor
            Aug 8 '13 at 15:12





















          10














          To only sort on the first column you should do:



          sort -n -s -k1,1


          From Unix and Linux System Administration Handbook




          sort accepts the key specification -k3 (rather than -k3,3), but it probably doesn’t do what you expect. Without the terminating field number, the sort key continues to the end of the line







          share|improve this answer


























          • Not working for me, I need to add the -s option as Cascabel pointed out.

            – Jean Paul
            Jan 7 at 17:08











          • @JeanPaul you are right, the documentation for -s says "This option maintains the original record order of records that have an equal key."

            – tidbeck
            Jan 9 at 10:09



















          2














          None of the provided answers work generally for me.



          Both sort -s -k 2 file1 and sort -n -k1,1 do additional sorting with this file:



          # cat file1
          3 3 5
          3 2 3
          1 4 7
          0 1 2
          3 2 1


          I just had to do this exact thing and ended up using a shell loop.
          This solution might not work well on a very large file because the entire
          file needs to be read for each unique value in the sorted column.



          Here the file is sorted on column 2 only.



          # awk '{print $2}' file1 | sort | uniq | while read index
          do
          awk -v var=$index '$2 == var { print $0}' file1
          done
          0 1 2
          3 2 3
          3 2 1
          3 3 5
          1 4 7





          share|improve this answer


























          • sort -s -k2,2 file1

            – plhn
            Feb 24 '17 at 10:22











          • The answer proposed by Cascabel is working but I think you missread it.

            – Jean Paul
            Jan 7 at 17:11











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fsuperuser.com%2fquestions%2f33362%2fhow-to-unix-sort-by-one-column-only%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          60














          Give this a try:



          sort -s -n -k 1,1


          The -s disables 'last-resort' sorting, which sorts on everything that wasn't part of a specified key.



          The -k 1 doesn't actually mean "this field and all of the following" in the context of numeric sort, as you can see if you try to sort on the second column. You're merely seeing ties broken by going to the rest of the line. In general, however, you need to specify -k 1,1 to sort only on field one.






          share|improve this answer


























          • You are right. This is exactly what I needed. Thanks!

            – ssn
            Aug 31 '09 at 16:31











          • is it possible to use join on the output of this sort?

            – MiNdFrEaK
            Sep 27 '12 at 20:13











          • @MiNdFrEaK: The requirement of join is that the input be sorted on the fields you're joining on. So sure, this output is sorted on the first field, and you can join on it.

            – Cascabel
            Sep 27 '12 at 21:49











          • I have 2 files, one having 2 columns, another having 1 column. The second file is sorted using sort -u. Now the task is I need to join this column with the first column of the first file, which is not sorted, so what will be the syntax? will this work? join -j 1 file2.txt sort -s -n -k 1 file1.txt?

            – MiNdFrEaK
            Sep 28 '12 at 4:10






          • 1





            The -k 1,1 (the ",1" part) doesn't work any better for me. What works is -s -k 1, with -n if you need it.

            – Totor
            Aug 8 '13 at 15:12


















          60














          Give this a try:



          sort -s -n -k 1,1


          The -s disables 'last-resort' sorting, which sorts on everything that wasn't part of a specified key.



          The -k 1 doesn't actually mean "this field and all of the following" in the context of numeric sort, as you can see if you try to sort on the second column. You're merely seeing ties broken by going to the rest of the line. In general, however, you need to specify -k 1,1 to sort only on field one.






          share|improve this answer


























          • You are right. This is exactly what I needed. Thanks!

            – ssn
            Aug 31 '09 at 16:31











          • is it possible to use join on the output of this sort?

            – MiNdFrEaK
            Sep 27 '12 at 20:13











          • @MiNdFrEaK: The requirement of join is that the input be sorted on the fields you're joining on. So sure, this output is sorted on the first field, and you can join on it.

            – Cascabel
            Sep 27 '12 at 21:49











          • I have 2 files, one having 2 columns, another having 1 column. The second file is sorted using sort -u. Now the task is I need to join this column with the first column of the first file, which is not sorted, so what will be the syntax? will this work? join -j 1 file2.txt sort -s -n -k 1 file1.txt?

            – MiNdFrEaK
            Sep 28 '12 at 4:10






          • 1





            The -k 1,1 (the ",1" part) doesn't work any better for me. What works is -s -k 1, with -n if you need it.

            – Totor
            Aug 8 '13 at 15:12
















          60












          60








          60







          Give this a try:



          sort -s -n -k 1,1


          The -s disables 'last-resort' sorting, which sorts on everything that wasn't part of a specified key.



          The -k 1 doesn't actually mean "this field and all of the following" in the context of numeric sort, as you can see if you try to sort on the second column. You're merely seeing ties broken by going to the rest of the line. In general, however, you need to specify -k 1,1 to sort only on field one.






          share|improve this answer















          Give this a try:



          sort -s -n -k 1,1


          The -s disables 'last-resort' sorting, which sorts on everything that wasn't part of a specified key.



          The -k 1 doesn't actually mean "this field and all of the following" in the context of numeric sort, as you can see if you try to sort on the second column. You're merely seeing ties broken by going to the rest of the line. In general, however, you need to specify -k 1,1 to sort only on field one.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 18 '13 at 15:54

























          answered Aug 31 '09 at 16:28









          CascabelCascabel

          1,0451014




          1,0451014













          • You are right. This is exactly what I needed. Thanks!

            – ssn
            Aug 31 '09 at 16:31











          • is it possible to use join on the output of this sort?

            – MiNdFrEaK
            Sep 27 '12 at 20:13











          • @MiNdFrEaK: The requirement of join is that the input be sorted on the fields you're joining on. So sure, this output is sorted on the first field, and you can join on it.

            – Cascabel
            Sep 27 '12 at 21:49











          • I have 2 files, one having 2 columns, another having 1 column. The second file is sorted using sort -u. Now the task is I need to join this column with the first column of the first file, which is not sorted, so what will be the syntax? will this work? join -j 1 file2.txt sort -s -n -k 1 file1.txt?

            – MiNdFrEaK
            Sep 28 '12 at 4:10






          • 1





            The -k 1,1 (the ",1" part) doesn't work any better for me. What works is -s -k 1, with -n if you need it.

            – Totor
            Aug 8 '13 at 15:12





















          • You are right. This is exactly what I needed. Thanks!

            – ssn
            Aug 31 '09 at 16:31











          • is it possible to use join on the output of this sort?

            – MiNdFrEaK
            Sep 27 '12 at 20:13











          • @MiNdFrEaK: The requirement of join is that the input be sorted on the fields you're joining on. So sure, this output is sorted on the first field, and you can join on it.

            – Cascabel
            Sep 27 '12 at 21:49











          • I have 2 files, one having 2 columns, another having 1 column. The second file is sorted using sort -u. Now the task is I need to join this column with the first column of the first file, which is not sorted, so what will be the syntax? will this work? join -j 1 file2.txt sort -s -n -k 1 file1.txt?

            – MiNdFrEaK
            Sep 28 '12 at 4:10






          • 1





            The -k 1,1 (the ",1" part) doesn't work any better for me. What works is -s -k 1, with -n if you need it.

            – Totor
            Aug 8 '13 at 15:12



















          You are right. This is exactly what I needed. Thanks!

          – ssn
          Aug 31 '09 at 16:31





          You are right. This is exactly what I needed. Thanks!

          – ssn
          Aug 31 '09 at 16:31













          is it possible to use join on the output of this sort?

          – MiNdFrEaK
          Sep 27 '12 at 20:13





          is it possible to use join on the output of this sort?

          – MiNdFrEaK
          Sep 27 '12 at 20:13













          @MiNdFrEaK: The requirement of join is that the input be sorted on the fields you're joining on. So sure, this output is sorted on the first field, and you can join on it.

          – Cascabel
          Sep 27 '12 at 21:49





          @MiNdFrEaK: The requirement of join is that the input be sorted on the fields you're joining on. So sure, this output is sorted on the first field, and you can join on it.

          – Cascabel
          Sep 27 '12 at 21:49













          I have 2 files, one having 2 columns, another having 1 column. The second file is sorted using sort -u. Now the task is I need to join this column with the first column of the first file, which is not sorted, so what will be the syntax? will this work? join -j 1 file2.txt sort -s -n -k 1 file1.txt?

          – MiNdFrEaK
          Sep 28 '12 at 4:10





          I have 2 files, one having 2 columns, another having 1 column. The second file is sorted using sort -u. Now the task is I need to join this column with the first column of the first file, which is not sorted, so what will be the syntax? will this work? join -j 1 file2.txt sort -s -n -k 1 file1.txt?

          – MiNdFrEaK
          Sep 28 '12 at 4:10




          1




          1





          The -k 1,1 (the ",1" part) doesn't work any better for me. What works is -s -k 1, with -n if you need it.

          – Totor
          Aug 8 '13 at 15:12







          The -k 1,1 (the ",1" part) doesn't work any better for me. What works is -s -k 1, with -n if you need it.

          – Totor
          Aug 8 '13 at 15:12















          10














          To only sort on the first column you should do:



          sort -n -s -k1,1


          From Unix and Linux System Administration Handbook




          sort accepts the key specification -k3 (rather than -k3,3), but it probably doesn’t do what you expect. Without the terminating field number, the sort key continues to the end of the line







          share|improve this answer


























          • Not working for me, I need to add the -s option as Cascabel pointed out.

            – Jean Paul
            Jan 7 at 17:08











          • @JeanPaul you are right, the documentation for -s says "This option maintains the original record order of records that have an equal key."

            – tidbeck
            Jan 9 at 10:09
















          10














          To only sort on the first column you should do:



          sort -n -s -k1,1


          From Unix and Linux System Administration Handbook




          sort accepts the key specification -k3 (rather than -k3,3), but it probably doesn’t do what you expect. Without the terminating field number, the sort key continues to the end of the line







          share|improve this answer


























          • Not working for me, I need to add the -s option as Cascabel pointed out.

            – Jean Paul
            Jan 7 at 17:08











          • @JeanPaul you are right, the documentation for -s says "This option maintains the original record order of records that have an equal key."

            – tidbeck
            Jan 9 at 10:09














          10












          10








          10







          To only sort on the first column you should do:



          sort -n -s -k1,1


          From Unix and Linux System Administration Handbook




          sort accepts the key specification -k3 (rather than -k3,3), but it probably doesn’t do what you expect. Without the terminating field number, the sort key continues to the end of the line







          share|improve this answer















          To only sort on the first column you should do:



          sort -n -s -k1,1


          From Unix and Linux System Administration Handbook




          sort accepts the key specification -k3 (rather than -k3,3), but it probably doesn’t do what you expect. Without the terminating field number, the sort key continues to the end of the line








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 9 at 10:10

























          answered Oct 16 '12 at 13:59









          tidbecktidbeck

          1,1151215




          1,1151215













          • Not working for me, I need to add the -s option as Cascabel pointed out.

            – Jean Paul
            Jan 7 at 17:08











          • @JeanPaul you are right, the documentation for -s says "This option maintains the original record order of records that have an equal key."

            – tidbeck
            Jan 9 at 10:09



















          • Not working for me, I need to add the -s option as Cascabel pointed out.

            – Jean Paul
            Jan 7 at 17:08











          • @JeanPaul you are right, the documentation for -s says "This option maintains the original record order of records that have an equal key."

            – tidbeck
            Jan 9 at 10:09

















          Not working for me, I need to add the -s option as Cascabel pointed out.

          – Jean Paul
          Jan 7 at 17:08





          Not working for me, I need to add the -s option as Cascabel pointed out.

          – Jean Paul
          Jan 7 at 17:08













          @JeanPaul you are right, the documentation for -s says "This option maintains the original record order of records that have an equal key."

          – tidbeck
          Jan 9 at 10:09





          @JeanPaul you are right, the documentation for -s says "This option maintains the original record order of records that have an equal key."

          – tidbeck
          Jan 9 at 10:09











          2














          None of the provided answers work generally for me.



          Both sort -s -k 2 file1 and sort -n -k1,1 do additional sorting with this file:



          # cat file1
          3 3 5
          3 2 3
          1 4 7
          0 1 2
          3 2 1


          I just had to do this exact thing and ended up using a shell loop.
          This solution might not work well on a very large file because the entire
          file needs to be read for each unique value in the sorted column.



          Here the file is sorted on column 2 only.



          # awk '{print $2}' file1 | sort | uniq | while read index
          do
          awk -v var=$index '$2 == var { print $0}' file1
          done
          0 1 2
          3 2 3
          3 2 1
          3 3 5
          1 4 7





          share|improve this answer


























          • sort -s -k2,2 file1

            – plhn
            Feb 24 '17 at 10:22











          • The answer proposed by Cascabel is working but I think you missread it.

            – Jean Paul
            Jan 7 at 17:11
















          2














          None of the provided answers work generally for me.



          Both sort -s -k 2 file1 and sort -n -k1,1 do additional sorting with this file:



          # cat file1
          3 3 5
          3 2 3
          1 4 7
          0 1 2
          3 2 1


          I just had to do this exact thing and ended up using a shell loop.
          This solution might not work well on a very large file because the entire
          file needs to be read for each unique value in the sorted column.



          Here the file is sorted on column 2 only.



          # awk '{print $2}' file1 | sort | uniq | while read index
          do
          awk -v var=$index '$2 == var { print $0}' file1
          done
          0 1 2
          3 2 3
          3 2 1
          3 3 5
          1 4 7





          share|improve this answer


























          • sort -s -k2,2 file1

            – plhn
            Feb 24 '17 at 10:22











          • The answer proposed by Cascabel is working but I think you missread it.

            – Jean Paul
            Jan 7 at 17:11














          2












          2








          2







          None of the provided answers work generally for me.



          Both sort -s -k 2 file1 and sort -n -k1,1 do additional sorting with this file:



          # cat file1
          3 3 5
          3 2 3
          1 4 7
          0 1 2
          3 2 1


          I just had to do this exact thing and ended up using a shell loop.
          This solution might not work well on a very large file because the entire
          file needs to be read for each unique value in the sorted column.



          Here the file is sorted on column 2 only.



          # awk '{print $2}' file1 | sort | uniq | while read index
          do
          awk -v var=$index '$2 == var { print $0}' file1
          done
          0 1 2
          3 2 3
          3 2 1
          3 3 5
          1 4 7





          share|improve this answer















          None of the provided answers work generally for me.



          Both sort -s -k 2 file1 and sort -n -k1,1 do additional sorting with this file:



          # cat file1
          3 3 5
          3 2 3
          1 4 7
          0 1 2
          3 2 1


          I just had to do this exact thing and ended up using a shell loop.
          This solution might not work well on a very large file because the entire
          file needs to be read for each unique value in the sorted column.



          Here the file is sorted on column 2 only.



          # awk '{print $2}' file1 | sort | uniq | while read index
          do
          awk -v var=$index '$2 == var { print $0}' file1
          done
          0 1 2
          3 2 3
          3 2 1
          3 3 5
          1 4 7






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 30 '16 at 18:49









          MJH

          1,02941018




          1,02941018










          answered Dec 30 '16 at 17:47









          user680341user680341

          211




          211













          • sort -s -k2,2 file1

            – plhn
            Feb 24 '17 at 10:22











          • The answer proposed by Cascabel is working but I think you missread it.

            – Jean Paul
            Jan 7 at 17:11



















          • sort -s -k2,2 file1

            – plhn
            Feb 24 '17 at 10:22











          • The answer proposed by Cascabel is working but I think you missread it.

            – Jean Paul
            Jan 7 at 17:11

















          sort -s -k2,2 file1

          – plhn
          Feb 24 '17 at 10:22





          sort -s -k2,2 file1

          – plhn
          Feb 24 '17 at 10:22













          The answer proposed by Cascabel is working but I think you missread it.

          – Jean Paul
          Jan 7 at 17:11





          The answer proposed by Cascabel is working but I think you missread it.

          – Jean Paul
          Jan 7 at 17:11


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • 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%2fsuperuser.com%2fquestions%2f33362%2fhow-to-unix-sort-by-one-column-only%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

          Plaza Victoria

          In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

          How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...