0

I am writing a code snippet where I store values from one file to another by grep. So in code below, I am reading from ./stats.txt and storing it two different files on the basis of what I 'grep'. I then want to read these two different files created i.e. out_base.txt and out_base1.txt and do mathematical operation on them. The operation needs floating point handling. I have installed bc and zsh but it still doesn't work:

base1=()
base2=()

grep "system.cpu.branchPred.condPredicted" ./stats.txt >>      out_base.txt
file="./out_base.txt"
while IFS=' ' read -r f1 f2 f3
do
base1=($f2) 
done <"$file"

grep "system.cpu.branchPred.condIncorrect" ./stats.txt >>    out_base1.txt
file="./out_base1.txt"
while IFS=' ' read -r f1 f2 f3
do
base2=($f2) 
done <"$file"

((diff=base1[0]-base2[0]))
echo "scale = 2; diff/base2[0]" | bc

I get 0 as my output. I understand I am losing the Floating Point value because bash doesn't handle it, but is there a way to handle this in my script ?

R_Moose
  • 103
  • 9
  • 1
    You're echoing variables' *names* into `bc`'s stdin, not their values. It has no way to see the shell variables' values. – Charles Duffy Apr 05 '19 at 03:27
  • 1
    ...which is to say, make it `echo "scale=2; $diff/${base2[0]}" | bc` and you're done. I'd argue that this isn't a problem with floating-point math at all (you already know you need to use bc), and is instead a problem with understanding variable expansion syntax (so that `echo` actually passes the values in question *into* `bc`). – Charles Duffy Apr 05 '19 at 03:27
  • (Others finding this question from searching on the title, btw, may find [BashFAQ #22](https://mywiki.wooledge.org/BashFAQ/022) pertinent). – Charles Duffy Apr 05 '19 at 03:29
  • 1
    BTW, why are you creating the temporary files at all, *or* using `while` loops when you only expect to read a single line? `read -r _ base1_ < <(grep "system.cpu.branchPred.condPredicted" ./stats.txt)` and there you are; there's no reason to create `out_base.txt` in the first place. – Charles Duffy Apr 05 '19 at 03:30
  • 1
    `bc <<<"scale=2; ($base1 - $base2) / $base2"`, rather; that way you don't try to do floating-point subtraction with shell builtins, which doesn't work any more than floating-point division does. – Charles Duffy Apr 05 '19 at 03:32
  • 1
    Beyond that -- without your `stats.txt`, nobody can actually *test* your code. When asking a question, please try to follow [mcve] guidelines; being "complete" and "verifiable" means you should include *everything needed* for someone to be able to see the same problem just by copying-and-pasting inside the question itself (whereas "minimal" asks that you include *only* the content necessary for the above). – Charles Duffy Apr 05 '19 at 03:35
  • Thanks for the comments. really helped me a lot with my script. I will take a note of these tips. Thanks – R_Moose Apr 05 '19 at 03:36
  • 1
    (Oops, I was missing a space earlier; should have been `read -r _ base1 _ < <(...)` to read the second word in the output of `...` into `base1`, discarding everything both before and after it -- apologies about that). – Charles Duffy Apr 05 '19 at 16:08

0 Answers0