Shell command to find highest number. But issue is double digit after decimal is not considered highest

Using shell script to find highest number out of the values mentioned in a variable.

For example

VAR=”2011.2/2009.1/2011.10″

Shell script should identify 2011.10 is highest of of three Output should be 2011.10

if VAR=”2011.10/2011.1″ output should be 2011.10

if VAR=”2010.1/2011.2″ output should be 2011.2

Issue here is that after decimal point double digit number are considered lower than single digit after decimal point.

Answer

What you request is more like version numbers comparison.

tr + sort + head solution:

v="2011.2/2009.1/2011.10"
tr '/' 'n' <<<"$v" | sort -rV | head -1
2011.10

Test cases:

v="2011.1/2011.2"
tr '/' 'n' <<<"$v" | sort -rV | head -1
2011.2

v="2011.1/2011.2/2011.101"
tr '/' 'n' <<<"$v" | sort -rV | head -1
2011.101

Leave a Reply

Your email address will not be published. Required fields are marked *