Compare float value with single decimal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare float value with single decimal
# 1  
Old 03-10-2011
Compare float value with single decimal

Code:
#!/bin/bash
filter_file=/export/home/alvin/test.txt
range1_count=0
range2_count=0
categorized(){
	value=$1
	if [$value -ge 5.1 && $value -le 10.0] # range 5.1 to 10.0
	then
		range1_count=`expr $range1+ 1`
	elif [$value -ge 10.1 && $value -le 15.0] # range 5.1 to 15.00
	then
		range2_count=`expr $range2+ 1`
	fi
}

#read txt file which contain lines of float with single decimal
loop_function(){
	while read line
	do
		categorized $line
	done < $filter_file
}

loop function

I got an issue about comparing two floating point with single decimal, by right -le and -ge is for integer value. So, what should i do?
# 2  
Old 03-10-2011
show you an example, it might help you
Code:
if [[ $(echo "6.5 > 4.5" | bc) == 1 ]]; then echo ok; fi
ok

# 3  
Old 03-10-2011
Code:
#!/bin/bash

declare $(awk '$1>=5.1 && $1<=10{r1++}$1>=10 && $1<=15{r2++}END{print "range1_count="r1,"range2_count="r2}' /export/home/alvin/test.txt)

# 4  
Old 03-10-2011
Quote:
Originally Posted by danmero
Code:
#!/bin/bash

declare $(awk '$1>=5.1 && $1<=10{r1++}$1>=10 && $1<=15{r2++}END{print "range1_count="r1,"range2_count="r2}' /export/home/alvin/test.txt)

May i know how to get the range1_count and range2_count?

I used
Code:
#!/bin/bash
declare $(awk '$1>=5.1 && $1<=10{r1++}$1>=10 && $1<=15{r2++}END{print "range1_count="r1,"range2_count="r2}' /export/home/alvin/test.txt)
echo range1_count
echo range2_count

output:
Code:
projadm@/alvin% ./count_v2.sh

projadm@/alvin%

No value display, may i know why?
# 5  
Old 03-10-2011
Quote:
Originally Posted by alvin0618
No value display, may i know why?
Yes Smilie
Quote:
Originally Posted by alvin0618
Code:
#!/bin/bash
declare $(awk '$1>=5.1 && $1<=10{r1++}$1>=10 && $1<=15{r2++}END{print "range1_count="r1,"range2_count="r2}' /export/home/alvin/test.txt)
echo $range1_count
echo $range2_count

# 6  
Old 03-10-2011
haha~~ i always forgot this $ symbol..
Thanks 丹, it works.
I am newbie in shell scripting, would you please to explain the method declare is what about ?
# 7  
Old 03-10-2011
You could work with the value * 10 eg:

Code:
value=$(echo "scale=0;$1*10/1" | bc)
if [ $value -ge 51 && $value -le 100] # range 5.1 to 10.0


Last edited by Chubler_XL; 03-10-2011 at 11:54 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare columns in a single file

i have the following files (all separated by tabs): file 1.txt 1 yes 2 no 3 yes 4 yes file 2.txt a no b no c yes d no i combine the above files in file 3 which looks like file 3.txt 1 yes a no 2 no b no 3 yes c yes 4 yes d no now, i need to compare the values between column 2... (3 Replies)
Discussion started by: msonoth
3 Replies

2. Shell Programming and Scripting

How to compare decimal values in bash?

Hi, I am facing some error while doing the comparision between 2 decimal values in bash. Pl help me out. I have tried using below scripts. But its giving me error. 1)amt=12.3 opn_amt=12.5 var=$(awk 'BEGIN{ print "'$amt'"<"'$opn_amt'" }') if ;then echo "correct" else echo "Wrong"... (3 Replies)
Discussion started by: Siba Tripathy
3 Replies

3. Shell Programming and Scripting

How compare decimal values?

I have 2 files say tp1.txt and tp2.txt having following data cat tp1.txt abc,2.20,IN20 acb,3.15,DN10 bca,3,RD10 cat tp2.txt alv,1.00,IN20 aaa,4.05,DD10 abb,5.50,RD12 i want to compare the values on 2nd field of both the file, if value of first tp1.txt is greater than value... (3 Replies)
Discussion started by: ranabhavish
3 Replies

4. Shell Programming and Scripting

bin bash decimal compare

I need decimal comparing with if. Check if apache version is less than 2.2.17. I tried this and not working. #!/bin/bash apachever=`/usr/local/apache/bin/httpd -v | head -1 | awk '{print $3}' |cut -d/ -f 2` if ]; then echo "Apache version less than 2.2.17" else ... (7 Replies)
Discussion started by: anil510
7 Replies

5. UNIX for Dummies Questions & Answers

compare decimal and integer values in if in bash shell

i need to do camparisions like the below. For the case when first=10 and second=9.9 the scripts displays process failed. I need to be able to convert the values to integer before doing the comparision. Like 9.9 should be rounded over to 10 before doing comparision. Please advice how can... (3 Replies)
Discussion started by: nehagupta
3 Replies

6. UNIX for Dummies Questions & Answers

How to compare null and space using single if condition

Hi I have a input file with many fields and each filed will be with in double quotes(""). i want to check fields contains balnk,null or space using condition using if. when i write code as below for if condition its not working a=`awk -F ',' '{gsub("\"", "", $1);'NF==0';printf $1}'... (3 Replies)
Discussion started by: jayakumarrt
3 Replies

7. UNIX for Dummies Questions & Answers

Compare 2 files for a single column and output differences

Hi, I have a column in 2 different files which i want to compare, and output the results to a different file. The columns are in different positions in those 2 files. File 1 the column is in position 10-15 File 2 the column is in position 15-20 Please advise Thanks (1 Reply)
Discussion started by: samit_9999
1 Replies

8. Shell Programming and Scripting

Compare integer value with decimal

Hi all, I have a issue... Is it possible to compare integer value with decimal value. If it is not possible,then how can i compare 2 decimal values in born shell.thanks! (3 Replies)
Discussion started by: MARY76
3 Replies

9. Programming

math.h: float ceilf(float x)

Good morning, I'm testing the use of ceilf: /*Filename: str.c*/ #include <stdio.h> #include <math.h> int main (void) { float ceilf(float x); int dev=3, result=0; float tmp = 3.444f; printf("Result: %f\n",ceilf(tmp)); return 0; } (1 Reply)
Discussion started by: jonas.gabriel
1 Replies

10. Shell Programming and Scripting

compare decimal numbers

Hi anyone, i need to compare two decimal numbers i thought that it could be do it with if but... :( So, i'm writing in csh and i really apreciate if anyone can help me if ( $ppl_kn <= $ppl_wb ) then echo "############# KNdiscount model has the lowest perplexity" set... (5 Replies)
Discussion started by: tmxps
5 Replies
Login or Register to Ask a Question