如何根据前一行中的模式使用HTTP代码计算行数

时间:2018-02-07 16:03:24

标签: http logging awk pattern-matching

我有日志文件请求和响应在不同的行。 第一行:

[29/01/2018 16:23:49.900] GET http://www.google-analytics.com/collect?tid=UA-1234567-3....

第二行:

 [29/01/2018 16:23:49.976] response: status = HTTP/1.0 200 OK, contentLength=35, contentType=image/gif, responseBodySize=35

我需要根据第一行的模式(例如UA-1234567-3和UA-1234567-4)为所有请求从第二行计算HTTP代码,如(200,500,400)

有人可以给我一个正确的awk或grep命令来执行此操作。谢谢!

1 个答案:

答案 0 :(得分:0)

如果您的实际输出与显示的示例相同,那么awk之后可能会对您有所帮助。

awk '/response: status/{match($0,/HTTP[^,]*/);print substr($0,RSTART+9,RLENGTH-9)}' Input_file

编辑: 此处还添加了非单一的衬垫表格,并附上说明:

awk '
/response: status/{                   ##Checking condition here if a line is having string response: status in it then do following:
  match($0,/HTTP[^,]*/);              ##Using match utility of awk where I am writing regex to match from string HTTP to till the first occurrence of comma.
  print substr($0,RSTART+9,RLENGTH-9) ##Printing the substring whose starting value is RSTART+9 and end value is RLENGTH+9, where RSTART and RLENGTH are out of the box variables of awk and their values will be set once a regex is matched in match keyword.
}
' Input_file                          ##Mentioning the Input_file name which awk has to parse/read.