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.

  • is the problem including values with multiple leading digits and with maybe negative values? there further might be the chance to have exponential notation in large, small and non-needed cases. left alone the fact that a float can be even a "NaN" and its relatives. Nov 12, 2019 at 15:28

1 Answer 1


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.


Keep in mind the details from the info sort page:

     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
        * 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
        * 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.
  • 7
    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... Jul 30, 2018 at 6:57
  • 3
    Had to use LC_ALL=C sort -g ... to get my sort to work. Dec 3, 2021 at 14:46
  • @AxelBregnsbo same here, my locale uses a comma as decimal separator, so I had to use LC_NUMERIC=C sort (ALL also works, of course). Thank you for pointing this out.
    – MayeulC
    Mar 15 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.