我有一个包含以下格式的csv
字段1,字段2,成功计数,失败计数
我写了一个awk脚本来计算Field 2值的Success count和Failure count的总和。这个脚本似乎工作正常。但突然间它变得奇怪了。 它甚至在开始计数之前增加2。所以结果将比实际大两倍。以下是我的剧本。
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;}'
然后我在执行时添加了成功打印。 它打印在下面。
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;}'
有人可以帮忙吗?
CSV如下所示。
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用于删除Windows换行符并用linux换行符替换它们。
Grep用于仅过滤POST请求。
根据@ glenn的答案,我试过这个。 见下面的输出
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
预期产出
HTTP Requests - POST, Failure : 0 Success :25 Total : 25
答案 0 :(得分:1)
使用awk时,你不需要sed和grep:
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
此外,第3列似乎成功且第4列失败,但您的代码中的变量已反转。这可以说明这种奇怪的行为吗?