How to sort lines by float number

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 margin-bottom:0;







up vote
3
down vote

favorite












I've such a file:



name: xxx --- time: 5.4 seconds
name: yyy --- time: 3.2 seconds
name: zzz --- time: 6.4 seconds
...


Now I want to sort this file by these float numbers to generate a new file as below:



name: yyy --- time: 3.2 seconds
name: xxx --- time: 5.4 seconds
name: zzz --- time: 6.4 seconds
...


I've tried the command awk 'print $5' myfile | sort -g but this will show me ONLY the float numbers.







share|improve this question



























    up vote
    3
    down vote

    favorite












    I've such a file:



    name: xxx --- time: 5.4 seconds
    name: yyy --- time: 3.2 seconds
    name: zzz --- time: 6.4 seconds
    ...


    Now I want to sort this file by these float numbers to generate a new file as below:



    name: yyy --- time: 3.2 seconds
    name: xxx --- time: 5.4 seconds
    name: zzz --- time: 6.4 seconds
    ...


    I've tried the command awk 'print $5' myfile | sort -g but this will show me ONLY the float numbers.







    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I've such a file:



      name: xxx --- time: 5.4 seconds
      name: yyy --- time: 3.2 seconds
      name: zzz --- time: 6.4 seconds
      ...


      Now I want to sort this file by these float numbers to generate a new file as below:



      name: yyy --- time: 3.2 seconds
      name: xxx --- time: 5.4 seconds
      name: zzz --- time: 6.4 seconds
      ...


      I've tried the command awk 'print $5' myfile | sort -g but this will show me ONLY the float numbers.







      share|improve this question













      I've such a file:



      name: xxx --- time: 5.4 seconds
      name: yyy --- time: 3.2 seconds
      name: zzz --- time: 6.4 seconds
      ...


      Now I want to sort this file by these float numbers to generate a new file as below:



      name: yyy --- time: 3.2 seconds
      name: xxx --- time: 5.4 seconds
      name: zzz --- time: 6.4 seconds
      ...


      I've tried the command awk 'print $5' myfile | sort -g but this will show me ONLY the float numbers.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 30 at 10:24









      Jeff Schaller

      30.7k846104




      30.7k846104









      asked Jul 30 at 6:02









      Yves

      686414




      686414




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          If using GNU sort or compatible, you can use its -g switch to do a general numeric sort:



          $ sort -g -k5,5 file
          name: yyy --- time: 3.2 seconds
          name: xxx --- time: 5.4 seconds
          name: zzz --- time: 6.4 seconds


          The -k5,5 tells sort to perform the sort on just the 5th column.



          Usage



          Keep in mind the details from the info sort page:




          '--general-numeric-sort'
          '--sort=general-numeric'
          Sort numerically, converting a prefix of each line to a long
          double-precision floating point number. *Note Floating point::.
          Do not report overflow, underflow, or conversion errors. Use the
          following collating sequence:

          * Lines that do not start with numbers (all considered to be
          equal).
          * NaNs ("Not a Number" values, in IEEE floating point
          arithmetic) in a consistent but machine-dependent order.
          * Minus infinity.
          * Finite numbers in ascending numeric order (with -0 and +0
          equal).
          * Plus infinity.

          Use this option only if there is no alternative; it is much slower
          than '--numeric-sort' ('-n') and it can lose information when
          converting to floating point.






          share|improve this answer



















          • 4




            Note that for numbers like 3.2, standard sort -n would be enough (provided the locale's radix character is that .). You'd need the -g GNU extension for numbers like 1e20, 0x20, inf...
            – Stéphane Chazelas
            Jul 30 at 6:57










          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          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%2f459257%2fhow-to-sort-lines-by-float-number%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          If using GNU sort or compatible, you can use its -g switch to do a general numeric sort:



          $ sort -g -k5,5 file
          name: yyy --- time: 3.2 seconds
          name: xxx --- time: 5.4 seconds
          name: zzz --- time: 6.4 seconds


          The -k5,5 tells sort to perform the sort on just the 5th column.



          Usage



          Keep in mind the details from the info sort page:




          '--general-numeric-sort'
          '--sort=general-numeric'
          Sort numerically, converting a prefix of each line to a long
          double-precision floating point number. *Note Floating point::.
          Do not report overflow, underflow, or conversion errors. Use the
          following collating sequence:

          * Lines that do not start with numbers (all considered to be
          equal).
          * NaNs ("Not a Number" values, in IEEE floating point
          arithmetic) in a consistent but machine-dependent order.
          * Minus infinity.
          * Finite numbers in ascending numeric order (with -0 and +0
          equal).
          * Plus infinity.

          Use this option only if there is no alternative; it is much slower
          than '--numeric-sort' ('-n') and it can lose information when
          converting to floating point.






          share|improve this answer



















          • 4




            Note that for numbers like 3.2, standard sort -n would be enough (provided the locale's radix character is that .). You'd need the -g GNU extension for numbers like 1e20, 0x20, inf...
            – Stéphane Chazelas
            Jul 30 at 6:57














          up vote
          3
          down vote



          accepted










          If using GNU sort or compatible, you can use its -g switch to do a general numeric sort:



          $ sort -g -k5,5 file
          name: yyy --- time: 3.2 seconds
          name: xxx --- time: 5.4 seconds
          name: zzz --- time: 6.4 seconds


          The -k5,5 tells sort to perform the sort on just the 5th column.



          Usage



          Keep in mind the details from the info sort page:




          '--general-numeric-sort'
          '--sort=general-numeric'
          Sort numerically, converting a prefix of each line to a long
          double-precision floating point number. *Note Floating point::.
          Do not report overflow, underflow, or conversion errors. Use the
          following collating sequence:

          * Lines that do not start with numbers (all considered to be
          equal).
          * NaNs ("Not a Number" values, in IEEE floating point
          arithmetic) in a consistent but machine-dependent order.
          * Minus infinity.
          * Finite numbers in ascending numeric order (with -0 and +0
          equal).
          * Plus infinity.

          Use this option only if there is no alternative; it is much slower
          than '--numeric-sort' ('-n') and it can lose information when
          converting to floating point.






          share|improve this answer



















          • 4




            Note that for numbers like 3.2, standard sort -n would be enough (provided the locale's radix character is that .). You'd need the -g GNU extension for numbers like 1e20, 0x20, inf...
            – Stéphane Chazelas
            Jul 30 at 6:57












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          If using GNU sort or compatible, you can use its -g switch to do a general numeric sort:



          $ sort -g -k5,5 file
          name: yyy --- time: 3.2 seconds
          name: xxx --- time: 5.4 seconds
          name: zzz --- time: 6.4 seconds


          The -k5,5 tells sort to perform the sort on just the 5th column.



          Usage



          Keep in mind the details from the info sort page:




          '--general-numeric-sort'
          '--sort=general-numeric'
          Sort numerically, converting a prefix of each line to a long
          double-precision floating point number. *Note Floating point::.
          Do not report overflow, underflow, or conversion errors. Use the
          following collating sequence:

          * Lines that do not start with numbers (all considered to be
          equal).
          * NaNs ("Not a Number" values, in IEEE floating point
          arithmetic) in a consistent but machine-dependent order.
          * Minus infinity.
          * Finite numbers in ascending numeric order (with -0 and +0
          equal).
          * Plus infinity.

          Use this option only if there is no alternative; it is much slower
          than '--numeric-sort' ('-n') and it can lose information when
          converting to floating point.






          share|improve this answer















          If using GNU sort or compatible, you can use its -g switch to do a general numeric sort:



          $ sort -g -k5,5 file
          name: yyy --- time: 3.2 seconds
          name: xxx --- time: 5.4 seconds
          name: zzz --- time: 6.4 seconds


          The -k5,5 tells sort to perform the sort on just the 5th column.



          Usage



          Keep in mind the details from the info sort page:




          '--general-numeric-sort'
          '--sort=general-numeric'
          Sort numerically, converting a prefix of each line to a long
          double-precision floating point number. *Note Floating point::.
          Do not report overflow, underflow, or conversion errors. Use the
          following collating sequence:

          * Lines that do not start with numbers (all considered to be
          equal).
          * NaNs ("Not a Number" values, in IEEE floating point
          arithmetic) in a consistent but machine-dependent order.
          * Minus infinity.
          * Finite numbers in ascending numeric order (with -0 and +0
          equal).
          * Plus infinity.

          Use this option only if there is no alternative; it is much slower
          than '--numeric-sort' ('-n') and it can lose information when
          converting to floating point.







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jul 30 at 6:54









          Stéphane Chazelas

          277k52511841




          277k52511841











          answered Jul 30 at 6:09









          slm♦

          232k65479649




          232k65479649







          • 4




            Note that for numbers like 3.2, standard sort -n would be enough (provided the locale's radix character is that .). You'd need the -g GNU extension for numbers like 1e20, 0x20, inf...
            – Stéphane Chazelas
            Jul 30 at 6:57












          • 4




            Note that for numbers like 3.2, standard sort -n would be enough (provided the locale's radix character is that .). You'd need the -g GNU extension for numbers like 1e20, 0x20, inf...
            – Stéphane Chazelas
            Jul 30 at 6:57







          4




          4




          Note that for numbers like 3.2, standard sort -n would be enough (provided the locale's radix character is that .). You'd need the -g GNU extension for numbers like 1e20, 0x20, inf...
          – Stéphane Chazelas
          Jul 30 at 6:57




          Note that for numbers like 3.2, standard sort -n would be enough (provided the locale's radix character is that .). You'd need the -g GNU extension for numbers like 1e20, 0x20, inf...
          – Stéphane Chazelas
          Jul 30 at 6:57












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f459257%2fhow-to-sort-lines-by-float-number%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)