我在Linux机器上并且有一个包含这些条目的日志文件,
...
但有时服务器已经死了,并且没有给我回复。
我必须得到那些没有响应的日志条目。我使用了GREP命令并创建了一个包含" LOG_ENTRY"行的新文本文件。至今。 (我的意思是这个文本文件不包含THREAD RUN行,只包含请求和响应行。)
示例:
预期产出:
有没有办法跟踪这些请求而没有响应?
答案 0 :(得分:1)
使用awk。这要求请求在响应之前,但很容易修复:
$ awk '$3=="RequestXML:"{a[$2]=$0} $3=="ResponseXML"{delete a[$2]} END{for(i in a)print a[i]}' file
20170824-185023.126 LOG_ENTRY_FOR_56823 RequestXML: /* SOME FANCY REQUEST XML TEXT HERE FOR 56823 BUT THERE IS NO RESPONSE*/
20170824-185026.421 LOG_ENTRY_FOR_56824 RequestXML: /* AS YOU CAN SEE, HERE IS ANOTHER REQEUST FOR 56824, NO RESPONSE FOR 56823*/
说明:
$ awk '
$3=="RequestXML:" { # request record
a[$2]=$0 # hash record to a, index with LOG_ENTRY_FOR string
}
$3=="ResponseXML" { # response record
delete a[$2] # delete matching request from a
}
END { # in the end
for(i in a) # loop all remaining requests
print a[i] # and output them
}' file
我用它进行测试:
20170823-164549.475 THREAD RUN
20170823-164550.482 THREAD RUN
20170823-164551.499 LOG_ENTRY_FOR_55897 RequestXML: /* SOME FANCY REQUEST XML TEXT HERE */
20170823-164552.454 LOG_ENTRY_FOR_55897 ResponseXML /* AND SOME FANCY RESPONSE TEXT HERE */
/* SOME THREAD RUN ROWS */
20170823-165438.766 LOG_ENTRY_FOR_55898 RequestXML: /* ANOTHER FANCY REQUEST XML TEXT HERE*/
20170823-165439.150 LOG_ENTRY_FOR_55898 ResponseXML /* ANOTHER FANCY RESPONSE XML TEXT HERE */
/* SOME MORE THREAD RUN ROWS */
But sometimes the server is dead and sends me no response.
20170824-185023.126 LOG_ENTRY_FOR_56823 RequestXML: /* SOME FANCY REQUEST XML TEXT HERE FOR 56823 BUT THERE IS NO RESPONSE*/
/* SOME THREAD RUN ROWS */
20170824-185026.421 LOG_ENTRY_FOR_56824 RequestXML: /* AS YOU CAN SEE, HERE IS ANOTHER REQEUST FOR 56824, NO RESPONSE FOR 56823*/
编辑要修复文本'RequestXML:'并且缩短的XML文本没有分隔,那里没有空间我们停止进行精确比较{{1而是使用正则表达式匹配$3=="..."
:
$3~/^.../