I need to multiply column1 and column3 data and need to compare it with column5. Need to check multiplication and Throw error if result is greater or less than column5 values, though difference of +/- 2 will be ok
Ex - if column1 has 2.4 and column3 has 3.5, it will be ok if column5 have value in range from row1(6.4 to 10.4), row2(248 to 252), row3(22634.8 to 22638.8) Only need to check multiplication result and Throw error. File has header too, but need to skip header while validating it.
By using the below code getting below wrong error code output
As multiplication result of $1*$3 is correct in $5. So i should not supposed to get error.
but still it is showing output as pasted below.
Ideally i should get error message only when multiplication result of $1 andd $3 is not correct and have deviation of more than +/- 2 when compared with $5
#!/bin/bash
awk -F"|" -v OFS="|" 'NR>1
{L = 0}
$5 > ($1 * $3 + 2) {L = 1}
$5 < ($1 * $3 - 2) {L = 2}
L {print "Error", L + 400, ": column1 and colmn3 does not match with column5,Field position 5, Linenumber:" NR, $0}
' a1.txt > b1.txt
Wrong output
Error|401|: column1 and colmn3 does not match with column5,Field position 5, Linenumber:1|a|b|c|d|e
2.4||3.5||8.4
5||50||250
6.55||3456||22636.8
For the below row, multiplication result of $1 * $3 is equal to $5, so i should not get an error message, but im getting with your code
Error should display only for the bad records rows with header in another file , those have multiplication result deviation of more than +/- 2.
a|b|c|d|e
2.4||3.5||8.4
Also i am using below code(.awk) to do the same thing with below input file
could you please help.
So with the above code i want to print only bad records in another file along with header and row number and not the both good and bad records.
Expected output
a||b||e
2.4||3.5||6.3 is bad, line number 1
5|50|247 is bad, line number 2
6.55||3456||22634.4 is bad, line number 3
Error 401 : column1 and colmn3 does not match with column5,Field position 5, Linenumber:1 a|b|c|d|e
Correct, isn't it?
Try
awk '
BEGIN {FS="|"
}
function abs(val) {return (val<0 ? -val : val)
}
NR == 1
abs($5-$1*$3) > 2 &&
NR > 1 {print $0 "\tis bad"
}
' file
a|b|c|d|e
2.4||3.5||6.3 is bad
5|50|247 is bad
6.55||3456||22634.4 is bad
That's obvious because NONE of them I believe are satisfying the conditions(since their difference is in range of +2 to -2, please check and let us know on same. Kindly check once and get back to us.
NOTE: Also try to encourage people by using THANKS button at the left most corner for their efforts.