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
Floating point error in C Hara High Level Programming 2 06-18-2008 02:43 AM
Rounding off the value of Floating point value damansingh Shell Programming and Scripting 7 05-21-2008 06:46 AM
Floating point exception !!! ssk01 Linux 2 05-14-2008 02:58 AM
floating point problem vijlak High Level Programming 4 03-08-2007 12:18 AM
Floating Point Division gsatch Shell Programming and Scripting 1 07-25-2002 01:03 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-21-2006
Registered User
 

Join Date: Dec 2006
Location: hyderabad
Posts: 102
floating point addition

hi,
I have a file like this
10.456
123.567
456.876
234.987
........
.......

What i want to do is ia have to add all those numbers and put the result in some other file.

Any help pls.

cheers
RRK
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-21-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
awk ' { num += $0 } END { printf("%f",num) }' file
or

Code:
echo $(tr '\n' '+' < file)"0" | bc
Reply With Quote
  #3 (permalink)  
Old 12-21-2006
Registered User
 

Join Date: Dec 2006
Location: hyderabad
Posts: 102
Hi,
When i use the following command,I am getting the following error.
echo $(tr '\n' '+' < numbers "0" ) | bc
error:Parsing error.
any help


cheers
RRK
Reply With Quote
  #4 (permalink)  
Old 12-21-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
String "0" should be outside $(...)
Code:
echo $(tr '\n' '+' < numbers )"0"  | bc
Code:
$(tr '\n' '+' < numbers )
will give output like this
For ex:
Code:
10.456+123.567+456.876+234.987+
To make above work ,add 0 to the above expression
Reply With Quote
  #5 (permalink)  
Old 12-21-2006
Registered User
 

Join Date: Dec 2006
Location: hyderabad
Posts: 102
hi,
I used the following command
echo $(tr '\n' '+' < numbers )"0" | bc
But now also it is giving the same error-"Parse error".

cheers
RRK
Reply With Quote
  #6 (permalink)  
Old 12-21-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Can you show the output of echo $(tr '\n' '+' < numbers )"0" ?
Reply With Quote
  #7 (permalink)  
Old 12-21-2006
Registered User
 

Join Date: Dec 2006
Location: hyderabad
Posts: 102
yes,here is the output :
10.45+11.34+12.87+13.45+14.45++0
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:20 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 Global Fact Book

Content Relevant URLs by vBSEO 3.2.0