Minus minus to plus

Hi there,

I have a problem with arithmetic ops in awk.
Here is what my script does right now.

while read nr val ; do
    case $nr in
    400) awk '$2~/eigenvectors/ {print $NF-'$val'};' input.txt >> output.txt;;
esac
done < frames.txt

I have a file named frames.txt with two columns (nr and val)

100   2.3
....
400   1.2
...
etc.   5.4

Additionally I have a file input.txt:

somename eigenvectors ..... 6.6

awk searches for the pattern "eigenvectors" and use the last field (which is a positive value usually) of input.txt and substract the corresponding value of frames.txt. In this case, 6.6 - 1.2 = 5.4

But sometimes $val and $NF are negative values ($NF= -6.6, $val=-1.2). This would mean that the arithmetic operation is: -6.6 - (-1.2) = -6.6 + 1.2 = -5.4 --> This would be the equation I'm aiming for, but instead I get as output:
-6.61.2

So something must be wrong with my operation. There is no substraction, the numbers are just strung together with the minus of 1.2 missing.

Could you help me to fix this problem please?
Thanks in advance

Try inserting spaces around the minus:

{print $NF - '$val'}
1 Like

Thanks, your solution works perfectly.