Ubuntu: Sort from terminal is not working properly


I am trying to sort a text file by its second column. All the text in the file is in word /t number format (a word, tab, and it's weight). I am trying to sort the file by it's second column, by weight but the results are not good. The file is in read & write.

The command I am using is sort -k2,2nr. (or change n for g or h).

No luck with either of them.

example of few lines, maybe the format matters:

00001930-n      7.18509e-06  00001740-n      4.35957e-05   00002137-n      1.13377e-05  00002452-n      7.73398e-06  00002684-n      0.000244948   00003553-n      3.98312e-05  00003993-n      3.98321e-07  

some results here: seems like the exponent is not taken care of. (these numbers are created using sort -k2,2nr filename.

02052204-n      3e-06    01975237-v      1.2e-06    02288155-v      1.2e-06    15105733-n      1.4e-06    00962135-a      1.6e-06    09719309-n      1.9e-06    02998269-a      2.5e-05    11938977-n      3.1e-06    14498972-n      3.6e-06    03341153-n      5.4e-07    01908703-n      6.5e-06    00257026-r      1.02e-06    08111599-n      1.03e-05    


You have the options swapped around, you need:

sort -k2,2gr filename  
  • k2,2 sets the key to sorting as the second field only; g,r is analogous to -g and -r, only difference is that this works only on the key when used like this


Solved. I'm sorry it had nothing to do with the sort function, but I'll leave an answer in case someone has the same problem.

The problem was the locale. I'm from Spain and even if my OS language and some more options were in en_US.UTF-8, the numeric was in es_ES.UTF-8. I changed that locale value and the sort worked. (I guess it has something to do with the punctuation mark.)

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »