Issue
I have a input file as shown below:
SR policy name state error code
1 backup01 successful 0
2 backup01 fail 13
3 backup01 fail 58
4 backup02 successful 0
5 backup02 successful 0
6 backup02 successful 0
I want to have an output where if any line for a particular policy (example backup01 has a line with "fail" state, it will only show "fail" state lines and will not show the "success" lines. Similarly where all lines of a policy (example backup02) have a state "successful" it will print all the "successful" state lines.
2 backup01 fail 13
3 backup01 fail 58
4 backup02 successful 0
5 backup02 successful 0
6 backup02 successful 0
I have tried using awk with little success, but not able to get ahead with a final solution.
awk '{if ($4 == 0) {print $0} else if($4 !=0 && $4 == 0) {print $0}}' input_file.txt
any other way using sed or other tool is also fine. ALso header from input file can also be ignored.
Solution
It can be done in two-stage processing of the same file. First pass, build up the status of the policy if it is going to be a "fail" or a "successful" and then exclude the contradicting lines in the next pass
awk '
FNR == NR && NR > 1 {
t = key[$2]
if ( $3 == "fail" || t == "fail" ) {
key[$2] = "fail"
} else {
key[$2] = $3
}
next
}
key[$2] == $3
' file file
Answered By - Inian Answer Checked By - Robin (WPSolving Admin)