【发布时间】:2014-11-13 06:35:26
【问题描述】:
我正在解析一个大约 10GB 的日志文件,并且需要通过 sed 将其提供给它以捕获一些输出。基于我将在 JavaScript 中使用的必要捕获段是:
s/method=""([^"]*)"".*path=""([^"]*)"".*accept=""([^"]*)""/"\1","\2","\3"/
不幸的是 sed(GNU sed 4.2.1,GnuWin32 版)在[^"]* 范围内苦苦挣扎。它拒绝匹配它们。我已经尝试过其他接受块的变体,[a-zA-Z0-9:\\/.]* 和类似的变体,但似乎总是在它错过的块内有新字符,实际上我可以接受引号之间的任何有效字符。由于 sed 的 * 例程是一个贪婪的实现,它往往在最后的“接受”项上也有问题,将日志条目中的所有其他项拉到最后。
我需要捕获引号之间的所有内容并忽略日志条目的其余部分。
我已经在这两天做了一些愚蠢的事情,如果不需要从带有 sed 的脚本执行的话,我可以直接在 python 中实现。任何正则表达式专家可以提供帮助吗?
编辑:
有关示例的额外信息,这在我的系统上没有匹配项,来自 GnuWin32.sourceforge.net 集合的 sed 4.2.1:sed -r 's/method=""([^"]*)"".*path=""([^"]*)"".*accept=""([^"]*)""/"\1","\2","\3"/' logfile
这会为某些条目生成匹配项:sed -r 's/^.*\method\=""([A-Z]*).*path=""([a-zA-Z0-9:\/]*).*accept=""(.*)"".*/"\1","\2","\3"/ logfile
以下是一些(略有删节但不是太多)行:
"server-01/1.2.3.4 time=""Wed Oct 29 05:59:59 GMT+00:00 2014"" method=""GET"" path=""/ourapp/foo/bar/AAA-123:1029"" status=""200"" message=""OK"" duration=""7"" query=""cc=1463648"" content_type=""application/json"" referer=""https://example.org/somewhere"" from=""foo@bar.com"" ip=""1.2.3.4"" agent=""Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36"" req_header_accept=""application/json, text/javascript, application/sord+xml; q=0.01"" req_header_accept-language=""en-US,en;q=0.8"" req_header_x-request-id=""29/Oct/2014:05:59:59.968a-abc123ABC"" req_header_x-forward=""1.2.3.4"" req_header_x-forwarded-for=""1.2.3.4"" ","2014-10-28T23:59:59.000-0000","someapp-01.a",production,1,"/home/someapp/log/ourapp-access.log","ut01-splunkidx18.i"
"server-01/1.2.3.4 time=""Wed Oct 29 05:59:59 GMT+00:00 2014"" method=""GET"" path=""/ourapp/foo/bar:AA9.1/ABC-123/record"" status=""200"" message=""OK"" duration=""73"" query=""view=includeFields"" content_type=""application/json"" from=""None"" ip=""1.2.3.4"" req_header_accept=""application/json"" req_header_x-request-id=""ab123-abc123-12345abc"" req_header_x-forward=""1.2.3.4"" req_header_x-forwarded-for=""1.2.3.4"" ","2014-10-28T23:59:59.000-0000","someapp-01.a",production,1,"/home/someapp/log/ourapp-access.log","ut01-splunkidx18.i"
"server-01/1.2.3.4 time=""Wed Oct 29 05:59:59 GMT+00:00 2014"" method=""HEAD"" path=""/ourapp/foo/bar:AA3.4/ABC-123/meta"" status=""200"" message=""OK"" duration=""21"" content_type=""application/json"" from=""foo@bar.com"" ip=""1.2.3.4"" agent=""Java/1.7.0_25"" req_header_accept=""application/json"" req_header_accept-language=""en"" req_header_cache-control=""no-cache"" req_header_x-request-id=""29/Oct/2014:05:59:59.882va-af527A"" req_header_x-forward=""1.2.3.4"" req_header_x-forwarded-for=""1.2.3.4"" ","2014-10-28T23:59:59.000-0000","someapp-01.a",production,1,"/home/someapp/log/ourapp-access.log","ut01-splunkidx18.i"
【问题讨论】:
-
[^"]*在 GNUsed中运行良好。请显示您用来调用sed的完成 命令。 (为了获得最佳结果,还请显示一些小样本输入和相应的所需输出。) -
信息已添加到原始帖子中,无法评论。