I am trying to sort a data file in descending order. The data file is given by three columns delimited by tabs; I want to order them in descending order for the third column with (the third column is given as a scientific notation in exponential value):

cat eII_surf.txt | sort -gr -k3

Somehow, this worked on a previous machine, but my new one does not seem to do the trick at all.

Here a simple example:

cat test.txt:

6.7 2.3e-12
5.0 3.4e-18
4.5 5.6e-16
4.2 2.1e-15
4.0 2.9e-17
2.4 2.5e-15
1.0 1.0e-17
0.5 1.0e-18

and cat test.txt | sort -gr -k2:

4.5 5.6e-16
5.0 3.4e-18
6.7 2.3e-12
4.2 2.1e-15
4.0 2.9e-17
2.4 2.5e-15
1.0 1.0e-17
0.5 1.0e-18

This is the output of locale:

  • None of the "failed sorted" is in the example input file. What do you get if you sort the example set of data you've shown us? What do you expect to get? Are the columns separated by a tab or by (multiple) spaces? – roaima Mar 18 '19 at 11:49
  • Is the question supposed to show the input and corresponding output? The data do not match. If you ran the command on a big file and copied parts of the data only, you should instead create a smaller input file and show the expected and actual output for this input. – Bodo Mar 18 '19 at 11:52

2.3e-12 would be understood as 2 in a locale where the decimal radix character is , (as it is in most of the non-English speaking world including your de_DE.utf8) where the number would need to be written 2,3e-12.

You could do:

LC_ALL=C sort -grk2 < your-file

To force numbers being interpreted in the English style.

In the C locale (the only one you would be guaranteed to find on any system), the decimal radix is . (conveniently for your input).

Note that sort has nothing to do with bash, it's a separate command. The -g option is a non-standard extension of the GNU implementation of sort.

| improve this answer | |
  • 3
    Thanks! That one seems to work. Would you recommend to set up LC_ALL=C in the bashrc as a new environment variable? – Lukas Fuchs Mar 18 '19 at 13:05
  • 5
    @LukasFuchs, no I wouldn't. You'd want to use LC_ALL=C only for commands where you don't want the behaviour to be affected by the user's localisation preferences. See What does "LC_ALL=C" do? – Stéphane Chazelas Mar 18 '19 at 13:08

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.