how to do decimal arithmetic in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to do decimal arithmetic in shell script
# 1  
Old 08-14-2012
MySQL how to do decimal arithmetic in shell script

hi,

I have a file with decimal/non-decimal values
Code:
$ cat b22
373 164 92 62 20 131 94 12 129 111 95 154 37 15 447 25 7.4 135 77 122 32 92 70 57 37 42 72 17 13 97 40 41 53 22 80 71 29 87 23 31 273 6.2 12K 43 44 45 22 11 7.7 13 18 173 36 20 18 13 56 67 104 53 5.4 241 19 13 3.8 38 14 31 329 16 155 17 81 74 18 45 110 58 23 4.0 13 33 6.9 32 68 28 22 34 139 60 31 22 101 12 13 105 14 19 109 11 6.6 65 44 9.0 132 221 16 16 166 11 76 46 38 42 18 87 68 29 5.4 3.4 41 32 27 406 275 35 24 26 49 5.2 19 146 60 41 12 7.9 77 14 15 24 29 98 24 98 187 56 96 9.5 46 60 24 25 169 196 13 37 16 50 76 12 181 12 74 8.2 26 112 21 12 86 41 1.5 2.3 130 8.2 415 36 24 18 52 54 17 11 8.1 9.2 101 15 85 44 43 107 21 19 111 6.2 26 42 15 39 116 33 49 60 20 127 7.6 55 84 87 34 32 22 21 66 102 29 14 20 7.7 27 22 12 25 138 45 6.4 40 18 34 103 22 18 64 106 121 7.7 16 75 9.3 7.9 23 4.8 28 40 63 23 50 3.4 14 1.5 24 5.4 9.2 16 73 34 150 13 41 120 127 260 4.3 147 14 29

I am trying to add these values
however, expr and let would fail
Code:
 
expr: non-numeric argument

Code:
 
-bash: let: a=1.2+2.8: syntax error: invalid arithmetic operator

I am trying to add these with this code
Code:
 
$ $sum=0; for i in `cat b22`
> do
> sum=`echo "$sum + $i" | bc`
> done; echo $sum;

but it throws the following error 256 times [same asthe no. of values in file b22]
Code:
(standard_in) 1: parse error

# 2  
Old 08-14-2012
Code:
 awk 'BEGIN { print ( 1.2 + 1.25 ) }'
2.45

# 3  
Old 08-14-2012
please provide a reply specific to the problem scenario depicted
# 4  
Old 08-14-2012
Quote:
Originally Posted by sam05121988
please provide a reply specific to the problem scenario depicted
Code:
awk '
BEGIN {FS=OFS=" "}
{
sum=0; n=0
for(i=1;i<=NF;i++)
     {sum+=$i; ++n}
     print "sum:"sum
}' temp_test

# 5  
Old 08-14-2012
Code:
$sum=0; for i in `cat b22`
do
sum=`echo "$sum + $i" | bc`
done; echo $sum;

would have worked if you had changed $sum=0 on the first line to sum=0
I assume that the variable sum was not set when you started so the first command turned into =0 when $sum expanded to an empty string.

However, it is pretty inefficient to invoke bc for each value you want to add. A much simpler way would be to use:
Code:
awk ' {	for (i=1; i<= NF; i++) sum+=$i }
END {print sum}' b22

which only invokes awk once instead of invoking bc 265 times. (Note that you have 265 values in b22; not 256.)
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 08-14-2012
If b22 is properly formatted (right now there is a 12K in there; how to interpret?) this is reasonably fast:
Code:
tr ' ' '+' <b22|bc

# 7  
Old 08-14-2012
For a pure shell solution, you can use ksh (precisely ksh93) which supports fractional arithmetic natively:
Code:
sum=0
for i in $(<b22); do
    sum=$((sum + i))
done
echo $sum

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh Arithmetic syntax error while comparing decimal numbers

Hello, I am having a problem when i execute following script on RHEL 6.4. Same script works fine on another machine where I have same version of RHEL and KSH. Below is the rpm and RHEL version. ossvm12(0)> rpm -qa | grep ksh ksh-20100621-19.el6.x86_64 ossvm12(0)> cat... (7 Replies)
Discussion started by: Adithya Gokhale
7 Replies

2. Shell Programming and Scripting

Shell arithmetic : operations on decimal points

i am having a varialbe a , which is input to my file i want to multiply this input with value .43, and assign it to variable b. i tried it as below: #!/bin/sh a=$1 b=`expr $1\*0.43` echo b=$b error : expr: non-integer argument Please tell me , how to do this. Thanks (10 Replies)
Discussion started by: rishifrnds
10 Replies

3. Shell Programming and Scripting

Arithmetic but keep 2 decimal places

I am trying to perform arithmetric, for example, to increment the value of variable $a (say 3) by 0.05 but when I tried the following expression let a=a+0.05 or a=$((a+0.05)) both returned 3.0499999999999998 I want to keep 2 decimal places so it returns 3.05 instead. (6 Replies)
Discussion started by: piynik
6 Replies

4. Shell Programming and Scripting

Help with Arithmetic calculations in Shell script

Hi, I need a help with arithmetic calculations in my script. I have two variables: a=17; b=1712 I want to perform ($a/$b)*100 with two decimals in the result. I tried with following: res=$((100*a/b)) res=`echo "scale=2; $a / $b" | bc` But I am not getting the decimal values.... (4 Replies)
Discussion started by: karumudi7
4 Replies

5. Shell Programming and Scripting

shell arithmetic least significant place

I need help on arithmetic root@server # hour=`date | awk {'print $4'} | cut -d: -f 1`; echo $hour 04 Now I subtract this result by 1 or 01 I get "3" as the answer. I need "03" as the answer, ie last two significant numbers should be there. root@server # hour=`date | awk {'print $4'} | cut... (3 Replies)
Discussion started by: anilcliff
3 Replies

6. Homework & Coursework Questions

Arithmetic Problem with shell script programming.

Hello everybody, I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input. I... (8 Replies)
Discussion started by: Florinel76
8 Replies

7. Shell Programming and Scripting

Arithmetic Problem with shell script programming.

Hello everybody, I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input. I... (1 Reply)
Discussion started by: Florinel76
1 Replies

8. Post Here to Contact Site Administrators and Moderators

Broken link FAQ date arithmetic with shell

page unix com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html Date Arithmetic with the Shell has link of www samag com/documents/s=8284/sam0307b/0307b.htm which is no longer. Is this the correct place to post this?:confused: and I got message... (1 Reply)
Discussion started by: dgerman
1 Replies

9. Shell Programming and Scripting

Arithmetic calculation on real numbers in Bourne Shell Script

I am begining to learn bourne shell and as a practice I have written a script which when given the purchase price and percentage of discount calculates the savings. I somehow cannot figure out why my script fails to do arthimatic calculation on real numbers. Could anyone look at the script... (5 Replies)
Discussion started by: Tirmazi
5 Replies

10. Fedora

Simple arithmetic in shell

Hey, I just wanted to know how one can write simple arithmetic like addition, subtraction, multiplication and division in shell-script. (14 Replies)
Discussion started by: #moveon
14 Replies
Login or Register to Ask a Question