The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find number of columns in a file rahul26 UNIX for Dummies Questions & Answers 12 05-20-2008 08:20 PM
Perl ? - How to find and print the lowest and highest numbers punched in by the user? some124one Shell Programming and Scripting 3 03-29-2008 09:04 AM
sort with highest wc ymf UNIX for Dummies Questions & Answers 1 03-26-2008 11:58 PM
How to find number of lines in a file somesh_p Shell Programming and Scripting 2 12-19-2007 06:15 PM
How to grep a number in a file to find them in another file ahjiefreak Shell Programming and Scripting 7 12-12-2007 03:01 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-21-2006
Registered User
 

Join Date: Feb 2006
Posts: 47
find the highest number in the file

Hi,

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

Eg :-

Quote:
12345
99555
65822
55
56566
..
..
.
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
Reply With Quote
Forum Sponsor
  #2  
Old 03-21-2006
Registered User
 

Join Date: Dec 2005
Location: India
Posts: 218
sort the file in ascending order and do tail -1 filename
or descending order and do head -1 filename

Gaurav
Reply With Quote
  #3  
Old 03-21-2006
Registered User
 

Join Date: Jan 2006
Posts: 59
Quote:
Originally Posted by systemali
I need to read through the file and find the number that is the greatest in them all.
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"
Reply With Quote
  #4  
Old 03-21-2006
Registered User
 

Join Date: Feb 2006
Posts: 47
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.
Reply With Quote
  #5  
Old 03-21-2006
Registered User
 

Join Date: Sep 2005
Location: india
Posts: 78
maxnum=$(sort -n a.txt |tail -1)
Reply With Quote
  #6  
Old 03-21-2006
Registered User
 

Join Date: Jan 2006
Posts: 59
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
Reply With Quote
  #7  
Old 03-21-2006
Registered User
 

Join Date: Feb 2006
Posts: 47
Hi Sirisha,

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

Quote:
eg :-
1.2.3.9
1.2.3.4
1.2.3.11
1.2.3.10
1.0.0.1
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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:56 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0