floating point shell variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting floating point shell variable
# 1  
Old 08-01-2007
floating point shell variable

I have a two files
>cat file1
jjjjj 10.345 6.673
ppp 9.000 5.883

>cat file2
mmm 80 10
jjjjj 10.305 6.873
ppp 9.000 5.883

I am reading file 1 line by line , and look for the string jjjj and then read the line in file 2 with jjjj
I want to get the flaoting point difference for colum2 and column 3 of file1 and file2, if both has the same column1.

I have the snippet code like this
while read line
do
name=`echo $line | awk ''{ print $1 }`
matchline=`grep -m 1 -e"$name" file2`
f1col2=`echo $line | awk ''{ print $2 }`
f2col2=`echo $matchline | awk ''{ print $2 }`
diffcol2=$(( $f1col2 - $f2col2 ))
echo $diffcol2
done < file1

However I get error at the line diffcol2=$(( $f1col2 - $f2col2 )),
10.345 - 10.305 : syntax error in expression (error token is ".10.345 - 10.305 ")

Do I ahve to do anything special for floating point substarction .
I tried bracing the variable like the following , but still di not work
diffcol2=$(( ${f1col2} - ${f2col2} ))
# 2  
Old 08-01-2007
I got it, it look like bash does not support floating point arithmetics

I have to use awk, like the following

diff=`echo $f1col1 $f2col2 | awk '{print $1-$2}'
# 3  
Old 08-01-2007
Hi.

Good work -- if one tool doesn't work, find another.

For small pieces of arithmetic, you can use bc:
Code:
#!/bin/sh

# @(#) s1       Demonstrate floating point arithmetic with bc.

set -o nounset
echo
echo "GNU bash $BASH_VERSION" >&2
bc --version | head -1 >&2
echo

echo 'scale=3 ; 10.345 - 10.305' |
bc

exit 0

which produces:
Code:
% ./s1

GNU bash 2.05b.0(1)-release
bc 1.06

.040

See man bc (and relative dc) for details ... cheers, drl
# 4  
Old 08-01-2007
Quote:
Originally Posted by jojan
I have a two files
>cat file1
jjjjj 10.345 6.673
ppp 9.000 5.883

>cat file2
mmm 80 10
jjjjj 10.305 6.873
ppp 9.000 5.883

I am reading file 1 line by line , and look for the string jjjj and then read the line in file 2 with jjjj
I want to get the flaoting point difference for colum2 and column 3 of file1 and file2, if both has the same column1.

I have the snippet code like this
while read line
do
name=`echo $line | awk ''{ print $1 }`
matchline=`grep -m 1 -e"$name" file2`
f1col2=`echo $line | awk ''{ print $2 }`
f2col2=`echo $matchline | awk ''{ print $2 }`
diffcol2=$(( $f1col2 - $f2col2 ))
echo $diffcol2
done < file1

However I get error at the line diffcol2=$(( $f1col2 - $f2col2 )),
10.345 - 10.305 : syntax error in expression (error token is ".10.345 - 10.305 ")

Do I ahve to do anything special for floating point substarction .
I tried bracing the variable like the following , but still di not work
diffcol2=$(( ${f1col2} - ${f2col2} ))
wouldn't it be easier to do it with 'awk':
Code:
nawk '
  FNR==NR { arr[$1]=$2; next }
  $1 in arr { printf("%s %f\n", arr[$1] - $2) }
' file1 file2

# 5  
Old 08-06-2007
Thansk , But I also have to print the originla values , with diffeences 0, incase I did not find a matching line.
# 6  
Old 08-06-2007
Quote:
Originally Posted by jojan
Thansk , But I also have to print the originla values , with diffeences 0, incase I did not find a matching line.
ok, so modify the proposed solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparison of floating point values in shell

Hi Everyone , Need a simple code here , I Have a number in a variable say $a=145.67 . This value changes everytime loop begins . I need to print a specific message as shown below when the above variable lies in a specific range i.e. 1.if $a lies within 100 and 200 , it should display... (2 Replies)
Discussion started by: robert89
2 Replies

2. Shell Programming and Scripting

Floating Point Numbers in c shell!

I have started using bash but this script which I am working on it, is in c chell. So here is my simple problem: set x = 0.4124\0.234 echo $x 0.4124.0.234 Same operation in Bash gives me correct result in my terminal. So there is something with my c shell that is causing this behaviour.... (8 Replies)
Discussion started by: dixits
8 Replies

3. Shell Programming and Scripting

IF statement on floating point variable in scientific format

Hi all, I'm struggling :wall: to make a an if statement on two variables in scientific format. e.g. a=1.23e+01 b=3.21e+02 I know that it can be done with bc if the variable are not in scientific format. But what to do in this case??? Thank you, (3 Replies)
Discussion started by: f_o_555
3 Replies

4. Shell Programming and Scripting

Add leading zeros in floating point variable

I need to add leading zeros in a floating point numbers. The length of the number should be 13 including decimal. The input number is changing so number of leading zeros is not fix. For example input output 216.000 000000216.000 1345.000 000001345.000 22345.500 ... (4 Replies)
Discussion started by: reeta_shri
4 Replies

5. Programming

Floating Point

Anyone help me i cant found the error of floating point if needed, i added the code complete #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> typedef struct { int hh; int mm; int ss; char nom; int punt; }cancion; typedef struct... (9 Replies)
Discussion started by: Slasho
9 Replies

6. Shell Programming and Scripting

floating point variables korn shell

Hi I'm not using Korn93 but want to use floating point variable. Is there any solution to do that ? thx for help. ---------- Post updated at 02:28 PM ---------- Previous update was at 12:38 PM ---------- I have the following peace of code: for n in `cat log.January.1.array` do ... (3 Replies)
Discussion started by: presul
3 Replies

7. Shell Programming and Scripting

Floating point to integer in variable length lines

Hi ! I'm looking for a way to transform certain floating point numbers in a one-line, variable length file to integers. I can do this in a crude way with sed : sed -e 's/0\.\(\):/\1:/g' -e 's/0\.0\(\):/\1:/g' -e 's/1\.000:/100:/g' myfile ... but this doesn't handle the rounding correctly. ... (3 Replies)
Discussion started by: jossojjos
3 Replies

8. Shell Programming and Scripting

How to compare floating point numbers in shell script?

How can we compare 2 floating point numbers in SHELL script? (11 Replies)
Discussion started by: dearanik
11 Replies

9. Programming

Floating point error in C

Hi, see the simple code below double i; i=8080.9940; printf(" val :%.30f\n",i); output i m getting is val :8080.993999999999700000000000000 when i m expecting val :8080.9940 what happens?how can i avoid it? thanks... (2 Replies)
Discussion started by: Hara
2 Replies

10. Shell Programming and Scripting

floating point variable in UNIX script

HI all, Can someone help me how to use or assign a floating point value in a variable in a unix script. I dont know how to use it. for example, I am to assign a variable VAR1 a value of 2.34 and evaluate if a certain vaue is greater or less than to it. That is, if var1 > 2.13 will do... (7 Replies)
Discussion started by: darry
7 Replies
Login or Register to Ask a Question