Linux command: SORT, according to the numerical value of the field

Example: file.txt file contents:

100 foo 2 bar 300 tuu 

Using 'sort -k 1.1 File.txt' the line order will not change, although we expect:

  2 bar 100 foo 300 tuu 

How can we sort a field consisting of numbers based on an absolute numerical value?

+62
sorting linux command
Jan 31 '11 at 20:59
source share
8 answers

Look at the page to sort ...

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

So here is an example ...

 sort -n filename 
+87
Jan 31 2018-11-21T00:
source share

Disclaimer: I know this question has been answered a long time ago, but it may be useful for some visitors to the question.

If you are sorting strings that are mixed text and numbers, for example sliding log file names, sorting with sort -n does not 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 this case, the -V option 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 

on the man page:

  -V, --version-sort natural sort of (version) numbers within text 
+43
Dec 02 '15 at 22:16
source share

Well, most of the other answers here relate to

 sort -n 

However, I'm not sure if this works for negative numbers. Here are the results that I get with sorting 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 clearly not ordered by numerical value.

Then, I think, a more accurate answer would be to use sort -n , but only if all values ​​are positive.

PS: Using sort -g returns only the same results for this example

Edit:

It seems that the language settings affect how the minus sign affects the order ( see here ). To get the correct results, I simply did:

 LC_ALL=C sort -n filename.txt 
+13
Feb 12 '13 at 7:45
source share

You need to use numerical sorting:

 sort -n -k 1,1 File.txt 
+6
Jan 31 2018-11-21T00:
source share

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

+2
Jan 31 2018-11-21T00:
source share

You must run the following command:

sort -n -k1 filename

That should do it :)

0
Mar 11 '16 at 22:07
source share

Use sort -nr to sort in descending order. Cm

Sort

See the previous Man page for more help.

-one
Nov 12 '15 at 11:20
source share
  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 
-3
Feb 03 '15 at 5:07
source share



All Articles