The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How can i prepare a file by comparing two other files? manmohanpv Shell Programming and Scripting 3 02-18-2008 04:58 AM
comparing two numbers with the decimals chittari Shell Programming and Scripting 4 06-28-2006 10:57 AM
comparing and replacing from one file to another naren_chella Shell Programming and Scripting 3 06-07-2006 10:02 AM
comparing password file Bosibus Shell Programming and Scripting 7 02-16-2005 03:48 PM
Comparing two numbers TabloMaxos UNIX for Dummies Questions & Answers 1 12-09-2003 10:25 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-20-2007
dusk2dawn dusk2dawn is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 2
comparing numbers in a file

Hello, I'm searching for a quick method to read numeric values from a file or a defined variable and identifying the largest number. For instance if the following numbers are in a file or defined to a variable:
09192007 09202007 09182007 09172007 09162007

What "short" method could be used to id "09202007" as being the largest number read in?

Thanks much!
  #2 (permalink)  
Old 09-20-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
I would use 'bc' to do comparisons.
  #3 (permalink)  
Old 09-20-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
Hi.

Command sort is very good at comparisons. It may take some manipulation ingenuity to get the numeric strings in an appropriate form, but once obtained, such a method would be very scalable ... cheers, drl
  #4 (permalink)  
Old 09-20-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Assuming that the numbers are in a variable, something like this will work:

Code:
a="09192007 09202007 09182007 09172007 09162007"
echo $a | tr ' ' '\n' | sort
09162007
09172007
09182007
09192007
09202007
You can tr the '\n' back to ' ' later if you want.

If the numbers are in a file, you can use a similar method:
Code:
tr ' ' '\n' < /file/with/numbers | sort 
09162007
09172007
09182007
09192007
09202007
  #5 (permalink)  
Old 09-21-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,508
just for that one line of numbers in that file

output:
Code:
# awk '{for ( i=1;i<=NF;i++) if ( temp < $i ){temp=$i }}END { print temp }' "file"
09202007
  #6 (permalink)  
Old 09-21-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
This is cheating a little bit. Very quick, though.
Code:
$ cat largest
#! /usr/bin/ksh
a="09192007 09202007 09182007 09172007 09162007"
eval set -s -A  n  $a
echo ${n[${#n[*]}-1]}
$ ./largest
09202007
$
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0