120

Example file.txt:

  100 foo
  2 bar
  300 tuu

When using sort -k 1,1 file.txt, the order of lines will not change, though we are expecting :

  2 bar
  100 foo
  300 tuu

How to sort a field consisting of numbers based on the absolute numerical value?

ivanleoncz
  • 5,662
  • 3
  • 49
  • 42
lukmac
  • 4,067
  • 8
  • 30
  • 34

8 Answers8

160

Take a peek at the man page for sort...

   -n, --numeric-sort
          compare according to string numerical value

So here is an example...

sort -n filename
Andrew White
  • 50,300
  • 17
  • 108
  • 132
  • 1
    Thank you all, folks! This is wired, because I looked over its man page back and forth several times and I didnt see that option. Ah, I was at a simplified man page. Damn! – lukmac Jan 31 '11 at 21:31
  • 1
    Please note that, for floating points, using `-g, --general-numeric-sort` could be more advisable. This further allows scientific notation e.g. 1.234E10 etc. – Herpes Free Engineer Apr 04 '18 at 11:20
117

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:

$ ls |sort -n
output.log.1
output.log.10
output.log.11
output.log.12
output.log.13
output.log.14
output.log.15
output.log.16
output.log.17
output.log.18
output.log.19
output.log.2
output.log.20
output.log.3
output.log.4
output.log.5
output.log.6
output.log.7
output.log.8
output.log.9

In that case option -V does the trick:

$ ls |sort -V
output.log.1
output.log.2
output.log.3
output.log.4
output.log.5
output.log.6
output.log.7
output.log.8
output.log.9
output.log.10
output.log.11
output.log.12
output.log.13
output.log.14
output.log.15
output.log.16
output.log.17
output.log.18
output.log.19
output.log.20

from man page:

   -V, --version-sort
          natural sort of (version) numbers within text
TMG
  • 2,359
  • 1
  • 14
  • 39
  • 1
    This tripped me up, so thank you! The other thing that messed me up, at least on my cygwin, is that even when piping the `ls` through `sed` substitutions to remove the letters and leave only numbers, apparently the colored output was affecting things as well. So running `ls --color=never` also made a difference. – Max Starkenburg Feb 11 '16 at 23:17
  • Awesome, `-V` is exactly what I was looking for. I should make a habit of looking through the man pages first. – srowley Dec 06 '19 at 16:33
  • Indeed, `-V` was the version I needed! – 9-Pin Feb 25 '21 at 20:01
18

Well, most other answers here refer to

sort -n

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:

-0.907928466796875
-0.61614990234375
1.135406494140625
0.48614501953125
-0.4140167236328125

Output:

-0.4140167236328125
0.48614501953125
-0.61614990234375
-0.907928466796875
1.135406494140625

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 example

Edit:

Looks like the locale settings affect how the minus sign affects the order (see here). In order to get proper results I just did:

LC_ALL=C sort -n filename.txt
pgilmon
  • 798
  • 1
  • 6
  • 10
7

You have to use the numeric sort option:

sort -n -k 1,1 File.txt
Kai Sternad
  • 20,524
  • 7
  • 44
  • 42
3

Use sort -n or sort --numeric-sort.

Roman Cheplyaka
  • 35,061
  • 6
  • 68
  • 115
1

You must do the following command:

sort -n -k1 filename

That should do it :)

amc
  • 702
  • 1
  • 7
  • 26
-1

Use sort -nr for sorting in descending order. Refer

Sort

Refer the above Man page for further reference

Aarish Ramesh
  • 5,321
  • 10
  • 49
  • 97
-4
    echo " Enter any values to sorting: "
read n
i=0;
t=0;
echo " Enter the n value: "
for(( i=0;i<n;i++ ))
do
read s[$i]
done
for(( i=0;i<n;i++ ))
do
for(( j=i+1;j<n;j++ ))
do
if [ ${s[$i]} -gt ${s[$j]} ]
then
t=${s[$i]}
s[$i]=${s[$j]}
s[$j]=$t
fi
done
done
for(( i=0;i<n;i++ ))
do
echo " ${s[$i]}  "
done