I'm trying to get the difference between two numbers which I have taken from two files. I think my code will make sense:
I've tried to make it work by two different methods, didn't work. What I get as an output is zero (0).
#method 1
difference_btwn_eng_hrs_MX3_122=$(($(sed -n '1p' engine_hours_new_MX3_122.txt)-$(sed -n '1p' engine_hours_old_MX3_122.txt)))
echo "$difference_btwn_eng_hrs_MX3_122"
#method 2
new=$(sed -n '1p' engine_hours_new_MX3_122.txt)
old=$(sed -n '1p' engine_hours_old_MX3_122.txt)
echo "$new $old" | awk '{print $new - $old}'
Eventually I will use the difference to set intervals for email updates.
The values inside the files are 511.786 (new) and 509.768 (old), and the error I get from the terminal is as follows:
line 40: 511.786-509.765: syntax error: invalid arithmetic operator (error token is ".786-509.765")
bc
, but solution withawk
doesn't work either? You can assign output to variable with command substitution$()
:difference=$(awk '{print $1-$2}' <<< "$new $old")
– jimmij Feb 12 '15 at 01:47$difference
in anif
statement?if [ "$difference" -ge "$limit"]; then echo "email sent out" fi
It doesn't like that I'm using a
– 3kstc Feb 12 '15 at 03:14float
in the condition, how could I bypass this?int_difference=${difference%.*}
– 3kstc Feb 12 '15 at 03:32