Reputation: 194
I'm trying to check filed on log file , line by line. For each line, check if specific filed is empty or field have wrong value . And print line with the error message
#!/bin/bash
LOG=/log/output.log
x=false
while read -r line; do
if
(echo $line | awk -F'|' '{if ($8=="") print "Application is empty"}') ||
(echo $line | awk -F'|' '{if ($9=="") print "Http request method is empty"}') ||
(echo $line | awk -F'|' '{if ($7=="") print "Operation is empty"}')
(echo $line | awk -F'|' '{if ($13 !~ /0|1|2/) print "result(0,1,2) has a wrong value"}')
then
echo $line
fi
done < "$LOG"
Actual results:
9f2b|EDR|V1|0|V1|2019-05-14|7||||2019-05-14T08:00:40.095Z|1|0|14|19|||XXXXX|||||||||897|||||||||5b8689707|||||||
Application is empty
9f2b|EDR|V1|0|V1|2019-05-14|7||||2019-05-14T08:00:40.095Z|18|0|||||XXXXX|||||||||1234|||||||||5b868007|||||||
Application is empty
42e2|EDR|V1|0|V1|2019-05-14|7||||2019-05-14T08:00:42.532Z|22|0|||||XXXXX|||||||||235|||||||||3b6959ae||||||||
Application is empty
83ac|EDR|V1|0|V1|2019-05-14|7||||2019-05-14T08:00:42.758Z|8|0|||||XXXXX|||||||||789|||||||||5945548f|||||
Expected Result:
Application is empty
Operation is empty
Http request method is empty
83ac|EDR|V1|0|V1|2019-05-14|7||||2019-05-14T08:00:42.758Z|8|0|||||XXXXX|||||||||789|||||||||5945548f|||||
Upvotes: 0
Views: 201
Reputation: 212634
awk
reads every line of the file, so there's no need for your while loop with echo
s. Just do:
awk -F\| ' {b=1}
$8=="" { print "Application is empty"; b=0 }
$9=="" { print "Http request method is empty"; b=0 }
$7=="" { print "Operation is empty"; b=0 }
$13 !~ /0|1|2/ {print "result(0,1,2) has a wrong value"; b=0 }
b
' /log/output.log
The main problem with your approach is that the command you are evaluating with if
always succeeds, so you always print the line. To make awk
fail, you would have to add exit statements to each of the awk statements. (eg echo $line | awk -F'|' '$8=="" {print "Application is empty"; exit 1 }'
This will print multiple error messages if a line fails multiple conditions, which yours will not do if you add the exit statements and short circuit the ||
operators.
If you want to print only one error message, you could do:
awk -F\| '
$8=="" { print "Application is empty"; next }
$9=="" { print "Http request method is empty"; next }
$7=="" { print "Operation is empty"; next }
$13 !~ /0|1|2/ {print "result(0,1,2) has a wrong value"; next }
' /log/output.log
Or, if you want to print multiple error messages, but all on one line, you could do:
awk -F\| ' {s=""}
$8=="" { printf "Application is empty"; s="; " }
$9=="" { printf "%sHttp request method is empty", s; s="; " }
$7=="" { printf "%sOperation is empty", s; s="; " }
$13 !~ /0|1|2/ {printf "%sresult(0,1,2) has a wrong value", s; s="; " }
s { print ""}
!s
' /log/output.log
Upvotes: 2