Example: content of File.txt:
100 foo
2 bar
300 tuu
When using 'sort -k 1,1 File.txt', the order of lines will not changed, though we are expecting :
2 bar
100 foo
300 tuu
How can we sort a field consisting numbers based on the absolute numerical value?
You must do the following command:
sort -n -k1 filename
That should do it :)
Take a peek at the man page for sort...
So here is an example...
Well, most other answers here refer to
However, I'm not sure this works for negative numbers. Here are the results I get with sort version 6.10 on Fedora 9.
Input file:
Output:
Which is obviously not ordered by numeric value.
Then, I guess that a more precise answer would be to use
sort -n
but only if all the values are positive.P.S.: Using
sort -g
returns just the same results for this exampleEdit:
Looks like the locale settings affect how the minus sign affects the order (see here). In order to get proper results I just did:
You have to use the numeric sort option:
If you are sorting strings that are mixed text & numbers, for example filenames of rolling logs then sorting with
sort -n
doesn't work as expected:In that case option
-V
does the trick:from man page:
Use
sort -n
orsort --numeric-sort
.