The UNIX and Linux Forums  

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 to add numbers email-lalit Shell Programming and Scripting 8 05-12-2008 03:34 PM
need top 3 numbers shary Shell Programming and Scripting 4 03-24-2008 10:58 PM
ksh and hex numbers JamesByars Shell Programming and Scripting 2 01-15-2008 12:36 PM
How to add numbers? pnxi Shell Programming and Scripting 7 09-11-2003 03:25 AM
Add some numbers! TalkShowHost Shell Programming and Scripting 3 05-15-2002 09:28 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-11-2008
Registered User
 

Join Date: Jun 2008
Posts: 33
how can i add two numbers

hi,

i am having one file which looks like the one below:

ABC1
***
1
4

ABC2
***
7
12

ABC3
***
0
34

...

i want to add the two numbers below each ABC and i want to find the largest out of this..

for example: in this case, 0+34=34 is the largest value and i want to get the result ABC3 into a variable.

can anyone help me...

thanks,
Krips.
Reply With Quote
Forum Sponsor
  #2  
Old 07-11-2008
Tytalus's Avatar
Registered User
 

Join Date: Jun 2003
Location: Scotland
Posts: 290
There are cleaner ways....but this should work:

Code:
#  cat infile
ABC1
***
1
4

ABC2
***
7
12

ABC3
***
0
34


#  <infile paste - - - - - | awk '{print $3+$4,$1}' | sort -nr | sed q
34 ABC3

OR
#  <infile paste - - - - - | awk '{print $3+$4,$1}' | sort -nr | sed q | cut -d" " -f2
ABC3
HTH
Reply With Quote
  #3  
Old 07-11-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,931
Something like this:
Code:
v=$(awk 'END { print v }
($3 + $4) > m {
  m = $3 + $4
  v = $1
  }' RS= file)

Last edited by radoulov; 07-11-2008 at 11:02 AM. Reason: too tired ...
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:25 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