I have input file with with x1 , x2 and x values, I want to check if x is midpoint between x1 and x2. But the comparison is failing.
sample input file
x1=20.9280 x2=20.9600 x=20.9440
x1=20.9280 x2=20.9600 x=20.9440
x1=22.7840 x2=22.8160 x=22.8000
Awk command
awk -F'[ =]' '{ if(($2 + $4)/2 != ($6)) print ($2 + $4)/2, " ", $6;}' sample
OUTPUT
20.944 20.9440
20.944 20.9440
22.8 22.8000
Comparison is failing due to extra zeros after decimal points. Please help to fix it.
0at the end of$6withsub(/0+$/, "", $6)before performing your comparisons.. but in general, comparing floats directly is risky, typically absolute difference between the two floats is checked against a small value, like0.00001- Sundeepx1=20.9280 x2=20.9600 x=20.9240(changed example) .. but with the code posted in question, all three lines were printed even though all of them have correct midpoint in final column.. that's why OP says they expected no output for example lines posted in question... - Sundeepawk -F'[ =]' '{gsub(/\./,"")} ($6*2) != ($2+$4)'- Sundeep!in the comparison. And yes, that conversion is what I put in my answer. - Ed Morton