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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-11-2008
kripssmart kripssmart is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 43
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.
  #2 (permalink)  
Old 07-11-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
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
  #3 (permalink)  
Old 07-11-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,915
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 03:02 PM.. Reason: too tired ...
  #4 (permalink)  
Old 12-30-2008
npk2210 npk2210 is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 6
How to sum values from two different files?

Hi

I am trying to add count values from two different files into one file. Could any body please suggest me best command to do this? My problem was as follows:

a.txt b.txt c.txt
10 20 30(needed)

i tried cat a.txt b.txt > c.txt (its not adding the values)


Thanks in advance..
Praveen
  #5 (permalink)  
Old 01-06-2009
silvermax silvermax is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 5
npk2210, if you are talking about adding the line count of the files this will work.

#!/bin/ksh

one=`wc -l a.txt|awk '{print $1}'`
two=`wc -l b.txt|awk '{print $1}'`

echo "$one $two" |awk '{sum=($1 + $2 ); print sum}' > c.txt

if your not wanting lines try man wc and see the options you have, hope this was what you were talking about.
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 04:14 PM.


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