Floating Division in Linux


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Floating Division in Linux
# 1  
Old 04-16-2008
CPU & Memory Floating Division in Linux

Hi everyone , have a great day
given below is the excerpt of code

HTML Code:
k=`grep -i success /var/seamless/spool/tdr/ERS_$date1$time1* | wc -l`;
l=`grep -i fail /var/seamless/spool/tdr/ERS_$date1$time1* | wc -l`;
m=`grep -i entertain /var/seamless/spool/tdr/ERS_$date1$time1* | wc -l`;
n=$(($k+$l))
o=$(($m/$n))
echo $k   $l   $m   $o
no $m is smaller then $n , hence $o will be something less then zero , but above given code doesn echo exact value of o ( should be something like 0.08766..) but it only echos 0
how can i get over this problem
Regards and thanks in anticipation
# 2  
Old 04-16-2008
shell uses integer arithmetic.

use bc or awk
Code:
echo "1.14 3.97" | awk '{print $1/$2}'
# if you need 20 deimals of precision try this:
echo "1.14 / 3.97" | bc -l

# 3  
Old 04-16-2008
CPU & Memory

Thanks Smilie , for your help
one last thing , i have this kinda text in my file

9
2
3
2
1
4
8
0
5
7
9
6




9
2
3
2
1
8
8
2
0
9
3
7




9
2
3
2
1
8
8
2
0
9
3
7




9
2
3
2
2
4
1
4
6
7
0
0




9
2
3
2
2
4
1
4
6
7
0
0




9
2
3
2
1
4
5
6
6
1
0
6


is it possible that i can arrange it something like
923214566106
92321.......
92321...
.......
and so one
i would be very much obliged
# 4  
Old 04-16-2008
Code:
nawk -v RS='' -v OFS='' '$1=$1' myFile.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Division of wc output

I have a function that outputs 3 lines for each result and I want to know how many results there are. so for example function | wc -l 24 but I want to see the result 8. so is there a easy way to divide the result? (5 Replies)
Discussion started by: yatici
5 Replies

2. UNIX for Dummies Questions & Answers

Help in division

hi, The below commands result only the whole number(not giving the decimal values). pandeeswaran@ubuntu:~$ echo 1,2,3,4|sed 's/,/\//g'|bc 0 pandeeswaran@ubuntu:~$ echo 1000,2,3|sed 's/,/\//g'|bc 166 How to make it to return the decimal values? Thanks (5 Replies)
Discussion started by: pandeesh
5 Replies

3. Shell Programming and Scripting

How to perform floating division in shell script?

I want to perform the below division operation in shell script and round the value. val1=6000 val2=5000 res=val1/val2 ----> 1.2---> Round to 2 Please help. (3 Replies)
Discussion started by: vel4ever
3 Replies

4. UNIX for Advanced & Expert Users

awk: division by zero

I received error "awk: division by zero" while executing the following statement. SunOS 5.10 Generic_142900-15 sun4us sparc FJSV,GPUZC-M echo 8 | awk 'END {printf ("%d\n",NR/$1 + 0.5);}' file1.lst awk: division by zero Can someone provide solution? Thanks Please use code... (11 Replies)
Discussion started by: kumar77
11 Replies

5. Shell Programming and Scripting

division by zero

Hello, I am searching for a way to calculate for example 10/100 within a shellscript and the result should be 0.1 and not just 0. Every alternative i tried just results 0 Thank you in advance 2retti (6 Replies)
Discussion started by: 2retti
6 Replies

6. UNIX for Dummies Questions & Answers

floating point error in linux + C

Here's a program and its pretty simple .It requires file handling and some calculations but on running it I am not getting the required result.It seems that the code outside the file read's outer while loop is not executing e.g the print statement is not being printed.Plz Help! #include<stdio.h>... (1 Reply)
Discussion started by: headrush
1 Replies

7. Shell Programming and Scripting

how to check for division by zero

i have a script that is doing the following: awk 'BEGIN {FS=","} ; {printf("%.10f",($5 - $2)/(3 * $3))}' data > test now some records in $3 contain zeroes. i don't want to remove those records. is it possible to check for division by zero and then write a "N/A" for that record in the o/p... (2 Replies)
Discussion started by: npatwardhan
2 Replies

8. UNIX for Dummies Questions & Answers

Problem in division

hi I am having two variables namely a=7 & b=8. I have to subtract these two variables. I am using the command c=`expr $a / $b` When I check the value of c, it comes out to be zero. Please help. Regards Rochit (9 Replies)
Discussion started by: rochitsharma
9 Replies

9. Filesystems, Disks and Memory

Cannot adjust division

I have a doubt with an error message, and i want to be sure if this is a normal situation or not. Situation: I was formating and installing a SCSI 36Gb HD with UNIX SCO 5.05, the problem happens when is making the division and filesystem on disk 1, and the message error is "Exit value 139... (1 Reply)
Discussion started by: jav_v
1 Replies

10. Shell Programming and Scripting

Floating Point Division

Does anyone have a simple way of doing floating point ("fp") division? For example, if I divide 3 by 5, I can get 0.6. The built-in calc (`bc`) will perform fp multiplication, but not division, at least not straight-up (i.e., starting bc and just typing in 3/5). I am trying to do this using... (1 Reply)
Discussion started by: gsatch
1 Replies
Login or Register to Ask a Question