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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Shell command to find highest number. But issue is double digit after decimal is not considered highest without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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
We are here to answer your question about Shell command to find highest number. But issue is double digit after decimal is not considered highest - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji