Extract the highest number out

Hi Gurus,

I've using HPUX B.11.23 U ia64 with shell = sh.

I've been having some problem get the highest number of this script.

Actually I wanted to get the highest number from this listing (TEST123 data and based on this highest number, there will be email being sent out.

For example, the number used is: 60 and if I used my method below, the script will display 3 numbers and send out an email 3 times.

How can it be fixed to display on the highest number (91) and send out email only 1 time?

Please kindly advice. Thanks a lot.

TEST123 (sample data):
17
65
84
54
0
60
39
46
2
91

for VAL1 in `cat TEST123`; do
  if [ $VAL1 -gt 60 ]; then
    echo $VAL1
    #perform mailx command ... mailx ...
   fi
done

Actual output:
65
84
91

Desired output
91

Regards,
Peter

Hi

sort -n file | tail -1

Guru.

Hi guruprasadpr,

Thanks for your response.

However, due to certain information that is tied to data in "TEST123", this data should not be manipulated, change order sequence, etc.

Anybody would be able to help me in getting the desired output?

Thanks a lot.

Regards,
Peter

You may use a "copy", then:

MAX_VAL=$( cat file | sort -n | tail -1 )

Peter...i did not get what you mean by manipulating data or changing order sequence..since the original file is in-tact.

However, to fix your original code:

max=0
for VAL1 in `cat TEST123`; do
  if [ $VAL1 -gt $max ]; then
    max=$VAL1
   fi
done
echo "The max value is $max"

Guru.

It is preferable to use a while read construction:

maxval=0
while read val
do 
  if [ $val -gt $maxval ]; then 
    maxval=$val
  fi
done < infile
echo $maxval

Hi guruprasadpr,

Sorry, I've misunderstood your reply.

I've tried your method and it's working.

Thanks a lot for your help.

Regards,
Peter

---------- Post updated at 09:56 AM ---------- Previous update was at 07:57 AM ----------

Hi All,

Thank you for your response and help.

I've tested all your methods and it's working as expected. :slight_smile:

Regards,
Peter