Skip to main content
1 of 2
Volker Siegel
  • 17.8k
  • 6
  • 56
  • 81

Use numeric sort

The command sort has an option -g (--general-numeric-sort) that can be used for comparisons on <, "less than" or >, "larger than", by finding the minimum or maximum.

These examples are finding the minimum:

$ printf '12.45\n10.35\n' | sort -g | head -1 10.35 

Supports E-Notation

It works with pretty general notation of floating point numbers, like with the E-Notation

$ printf '12.45E-10\n10.35\n' | sort -g | head -1 12.45E-10 

Note the E-10, making the first number 0.000000001245, indeed less than 10.35.

Can compare to infinity

The floating point standard, IEEE754, defines some special values. For these comparisons, the interesting ones are INF for infinity. There is also the negative infinity; Both are well defined values in the standard.

$ printf 'INF\n10.35\n' | sort -g | head -1 10.35 $ printf '-INF\n10.35\n' | sort -g | head -1 -INF 

To find the maximum use sort -gr instead of sort -g, reversing the sort order:

$ printf '12.45\n10.35\n' | sort -gr | head -1 12.45 
Volker Siegel
  • 17.8k
  • 6
  • 56
  • 81