我的Perl脚本提取文件日志(由Apache log4j创建),不知何故,下一行的正则表达式命令\.
没有将整个行存储到变量中;它工作正常,直到ErrorType我的变量消息为空,@nextline
应该包含所有整个下一行(任何字符任何字母任何特殊符号)。
我尝试了\. (\D+\S+)
Perl正则表达式:
while (<$fh>) {
my @fields = m{^
(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:[\d,]+)
\s (INFO | INFO\s | VERBOSE) \s
\[(?: SOAP | GUI )\s[(]User:["](\w+)",\sThreadId:\s\d+\)]
\s com.whatever.whichever.(\S+) \s \(\S+\.PYTHON\:\d+\) \s
- (?! \sUser )
\s (\D+\S+)
\. (\D+\S+) #the all entire next line
$}x
printf('$date=%s; $lovelforlogs=%s; $userid=%s; $methodused=%s; $Errortype=%s; $nextline=%s',@fields );
print "\n";
示例日志条目:
2014-12-10 12:25:13,688 INFO [SOAP (User:"userid", ThreadId: 11)] com.whatever.whichever.program.cache (myMethod.PYTHON:59) - CRITICAL ERROR
; hereSometest#: 368; some other#: 23
at org.JBOSS.xpath.compiler.XPathParser.error(XPathParser.PYTHON:610)
at org.JBOSS.xpath.compiler.XPathParser.initXPath(XPathParser.PYTHON:145)
at org.JBOSS.xpath.XPath.<init>(XPath.PYTHON:227)
at org.JBOSS.xalan.processor.StylesheetHandler.createXPath(StylesheetHandler.PYTHON:155)
at org.JBOSS.xalan.processor.XSLTAttributeDef.processEXPR(XSLTAttributeDef.PYTHON:763)
at org.JBOSS.xa
2015-01-21 12:23:51,681 INFO [SOAP (User:"userid", ThreadId: 83)] com.whatever.whichever.program.cache (myMethod.PYTHON:690) - ERROR
com.whatever.whicever.program.exceptions.InvalidParameterException: F20176 VALUE is WRONG [G00097]
at org.JBOSS.xpath.compiler.XPathParser.error(XPathParser.PYTHON:610)
at org.JBOSS.xpath.compiler.XPathParser.initXPath(XPathParser.PYTHON:145)
at org.JBOSS.xpath.XPath.<init>(XPath.PYTHON:227)
at org.JBOSS.xalan.processor.StylesheetHandler.createXPath(StylesheetHandler.PYTHON:155)
at org.JBOSS.xalan.processor.XSLTAttributeDef.processEXPR(XSLTAttributeDef.PYTHON:763)
at org.JBOSS.xa
2015-01-27 12:24:37,079 VERBOSE [SOAP (User:"userid", ThreadId: 70)] com.whatever.whichever.program.cache (myMethod.PYTHON:2066) - Unchecked error
AxisFault
at org.JBOSS.xpath.compiler.XPathParser.error(XPathParser.PYTHON:610)
at org.JBOSS.xpath.compiler.XPathParser.initXPath(XPathParser.PYTHON:145)
at org.JBOSS.xpath.XPath.<init>(XPath.PYTHON:227)
at org.JBOSS.xalan.processor.StylesheetHandler.createXPath(StylesheetHandler.PYTHON:155)
at org.JBOSS.xalan.processor.XSLTAttributeDef.processEXPR(XSLTAttributeDef.PYTHON:763)
at org.JBOSS.xa
结果应该是:
date=2014-12-10 12:25:13,688 lovelforlogs=INFO userid=userid methodused=myMethod Errortype=CRITICAL ERROR message=; hereSometest#: 368; some other#: 23
date=2014-12-10 12:25:13,688 lovelforlogs=INFO userid=userid methodused=myMethod Errortype=ERROR ERROR message=com.whatever.whicever.program.exceptions.InvalidParameterException: F20176 VALUE is WRONG [G00097]
date=2015-01-27 12:24:37,079 lovelforlogs=VERBOSE userid=userid methodused=myMethod Errortype=Unchecked error message=AxisFault
由于
答案 0 :(得分:1)
这里有你的逻辑缺陷 - 你正在做while ( <$fh> )
但是一次只能运行一行。所以没有正则表达式可以匹配。同样 - 如果您想使用多行正则表达式 - 您需要在标志中指定它。
米
将字符串视为多行。也就是说,更改“^”和“$”以匹配字符串第一行的开头和最后一行的结尾,以匹配字符串中每行的开头和结尾。
取值
将字符串视为单行。也就是说,改变“。”匹配任何字符,甚至是换行符,通常它不匹配。 一起使用,作为/ ms,他们让“。”匹配任何字符,同时仍允许“^”和“$”分别匹配字符串中的换行符之后和之前。
(但是在使用词法文件句柄和x
来指定你的RE方面做得很好 - 这使得它更加清晰)
继续发表评论 - 虽然while
是每行,但您可以作弊 - 如果您确定在模式匹配时您肯定只想要'下一行' - 请单独抓取它。
相反如何:
while ( <$fh> ) {
my @fields = #pattern;
my $next_line = <$fh>;
#etc.
}