Skip to main content
added 578 characters in body
Source Link
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 

Comparison operation

To implement the < ("less than") comparison, so it can be used in if etc, compare the minimum to one of the values. If the minimum is equal to the value, compared as text, it is less than the other value:

$ a=12.45; b=10.35 $ [ "$a" = "$(printf "$a\n$b\n" | sort -g | head -1)" ] $ echo $? 1 $ a=12.45; b=100.35 $ [ "$a" = "$(printf "$a\n$b\n" | sort -g | head -1)" ] $ echo $? 0 

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 

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 

Comparison operation

To implement the < ("less than") comparison, so it can be used in if etc, compare the minimum to one of the values. If the minimum is equal to the value, compared as text, it is less than the other value:

$ a=12.45; b=10.35 $ [ "$a" = "$(printf "$a\n$b\n" | sort -g | head -1)" ] $ echo $? 1 $ a=12.45; b=100.35 $ [ "$a" = "$(printf "$a\n$b\n" | sort -g | head -1)" ] $ echo $? 0 
Source Link
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