find the highest number in the file

Hi,

I have a file a.txt and it has values in it

Eg :-

I need to read through the file and find the number that is the greatest in them all.

Can any one assit me on this.

Thanks

sort the file in ascending order and do tail -1 filename
or descending order and do head -1 filename

Gaurav

This could be it, a easy method if your file size is small..

max=0

exec 4<&0
exec < file_of_nos

while read line
do
if [ $line -gt $m ]
then
max=$line
else
continue
fi
done

echo "Max no is:$max"

manthasirisha, Thank u so much,

Your solution works great, but it does not go beyond 5 lines,..what if i have 10 lines...how can i modify the script to suite that.

Thank you so much once again.

maxnum=$(sort -n a.txt |tail -1)

systemali,

i tried the script with an input file of more that 50 lines and it worked... check ur shell compatibility..

anyway, the easiest method is suggested above .. it should do the trick!

good luck,
Sirisha

Hi Sirisha,

Yes that worked just fine :), now i have another file which is in decimals :

Those are actually versions and I need to find the highest version number from the list above, How can this be accomplished.

Thanks for all your assistance.

maxversion=$(sed 's/\.//g' ver | sort -n | tail -1)

Hi Raom,

Thank you so much for that, But the problem is. It removes the (.) and then sorts it, Fine till there.

But i need to retain the (.)'s and sort it on them...How do i find the latest version from the below format :-

Thanks for every one's effort.

Cheers !!!

Can any one help on the above situation ??? Please...

Thanks

This should do

#! /bin/sh

maxversion=$(sed 's/\.//g' ver.txt | sort -n | tail -1)

echo $maxversion

while read line
do
        [ "${line//\./}" -eq "$maxversion" ] && echo "$line" && exit 0
done < ver.txt

Uses a sh only sed like construct.

Hi Vino,

Thank you so much for your reply :). I was awaiting it.

I have a small problem here, I am not sure what is causig this, but i get a syntax error

what could be wrong ?

Thanks Once again

A non-sed non-tail non-sort solution

#! /bin/sh
BASE=0

while read line
do
        VER=${line//\./}
        [ $VER -ge $BASE ] && BASE=$VER && high=$line
done < ver.txt
echo $high

Its not my day i guess :frowning:

It still displays.. error :-

Sorry for all the trouble

Show us the script that you are running. Like I mentioned before, the construct in VER=${line//\./} is a sh-only feature.

I could apparently see a bug being unnoticed from the answers posted above,
removing the meta-character; sorting and then displaying the last one.

the solution remains unanswered if there is a data like
3.0

version 3.0
it simply translates to 30 and no where that would be listed.

Try this one,

# !/usr/bin/ksh

ini=10000
sum=0
max=0
cnt=1
finalver=""

cal()
{
  sum=$(($sum + $1 \* $ini))
  ini=$(($ini / 10))
}

while read ver
do
ini=10000
sum=0
  echo $ver | sed 's/\./ /g' | while read v1 v2 v3 v4
  do
  if [ ! -z $v1 ]
  then
     cal $v1
     v1=""
  fi
  if [ ! -z $v2 ]
then
     cal $v2
     v2=""
  fi
  if [ ! -z $v3 ]
  then
     cal $v3
     v3=""
  fi
  if [ ! -z $v4 ]
  then
     cal $v4
     v4=""
  fi
  done
  if [ $sum -gt $max ]
  then
     max=$sum
     finalver=$ver
  fi
done < td
echo "Final Version: $finalver"
exit 0

the above one is not optimized and for sure it can be, its just a pointer to go with.
:slight_smile:

I am sorry every one....but i guess this has every one tricked :(...

none of the solutions mentioned above work :frowning:

Thank you once again for your time and patience in this matter.

why it didnt work?
what was the output you obtained ?
what was your expected?
could you please quantify your statements?
As something that is not quantified could have many meanings derived from something that still remains abstract!!!

With your input, I had tested with my script and it works fine.

Please throw some more light!

I'll try to explain again what i m trying to do :-

I have a file in the below format :-

NOw out of this i need to determine which is the latest version of them all. I have managed to yank of the file name "test*" and "tar.gz" from the list.

Now on the basis of version number i need to sort the latest file.

If there is any better way of doing that, Please let me know..

Your assistance is much appreciated.

test5.1.1.1.1.tar.gz
test56.1.1.1.1.tar.gz
test8.1.1.1.1.tar.gz

I guess the latest among these would be test56.1.1.1.1.tar.gz.