Reputation: 637
I Have a csv which contains below format
Field 1, Field 2, Success Count, Failure Count
I wrote a awk script to count the sum of Success count and Failure count of Field 2 values. This script seems to work fine. But suddenly it acts strange. It adds 2 before even start counting. So the result will be two more greater than actual. Below is my script.
sed 's/\r//' FileName.csv | grep 'HTTP Requests - POST' | awk -F "," '{failure += $3; success += $4} END {print "HTTP Requests - POST, Failure : " failure " Success : "success " Total : " success+failure;}'
Then I added success to print on execution. It prints below.
2
9
27
sed 's/\r//' FileName.csv | grep 'HTTP Requests - POST' | awk -F "," '{failure += $3; success += $4; print success;} END {print "HTTP Requests - POST, Failure : " failure " Success : "success " Total : " success+failure;}'
Can anyone help ?
CSV is looks like below.
OPERATOR,HTTP_METHOD,SUCCESS_COUNT,FAILURE_COUNT
CELL_01,HTTP Requests - POST,10,19
CELL_03,HTTP Requests - GET,12,17
CELL_04,HTTP Requests - POST,1,15
CELL_05,HTTP Requests - PUT,16,14
CELL_01,HTTP Requests - DELETE,19,13
CELL_03,HTTP Requests - POST,17,12
CELL_05,HTTP Requests - PUT,11,13
sed was used to remove windows line breaks and replace them with linux line breaks.
Grep was used to filter only POST Requests.
According to @glenn's answer below I tried this. See output below
awk -F "," '/\/GET/ { sub(/\r$/,""); failure += $3; success += $4; print failure; print success; print ""; } END {print "HTTP Requests - POST, Failure : " failure " Success : "success " Total : " success+failure}' Sandbox.csv
0
2
0
9
0
27
Expected Output
HTTP Requests - POST, Failure : 0 Success :25 Total : 25
Upvotes: 0
Views: 142
Reputation: 247012
You don't need sed and grep when using awk:
awk -F "," '
/POST/ {
sub(/\r$/,"")
failure += $3
success += $4
print success
}
END {print "HTTP Requests - POST, Failure : " failure " Success : "success " Total : " success+failure}
' FileName.csv
Also, it appears that column 3 is success and column 4 is failure, yet you have the variables reversed in your code. Could that account for the strange behaviour?
Upvotes: 1