我正在 grep 大量日志文件,查找某些带有 ORA-xxxxx 错误代码的 Oracle 相关错误。所有日志条目都以时间戳开头(例如:2017-11-29 23:51:46,372)。一些日志条目是多行的 - 比如那些带有 java 异常堆栈跟踪的日志条目,其中 ORA-xxxxx 代码位于带有时间戳的日志条目行的深处。
用于查找日志时间戳直至 ORA-xxxxx 代码的正则表达式是什么?
一旦我使上述正则表达式正常工作,接下来就是按日期对它们进行排序,以查找上次发生某个 ORA-xxxxx 错误的时间。
PS:grep 或 perl regex 命令就可以了。
谢谢你,
日志文件示例
2017-11-29 23:51:46,013 (Foo.java:67) FATAL - foo.bar()-got exception
during load of zzz javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:76) . . .
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505) Caused by: org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.doList(Loader.java:2235)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
at org.hibernate.loader.Loader.list(Loader.java:2124)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67)
... 16 more Caused by: java.sql.SQLException: [Mercury][Oracle JDBC Driver][Oracle]
ORA-04031: unable to allocate 3896 bytes of shared
memory ("shared pool","selec t licensekey0_.ID as ID...","sga
heap(1,0)","kglsim object batch")
答案1
这个 perl 单行程序可以完成这个工作:
perl -0777 -nE 'say $1 while(/(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d+(?:(?!ORA-\d+)(?!\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d+).)*ORA-\d+(?:(?!\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d+).)*)/sg)' file.txt
$1
每次找到组 1 ( ) 时,都会打印其内容。
选项:
-0777 : slurp mode
-n : add a loop around the script
-E : enable features ("say" in this case)
正则表达式:
/ : regex delimiter
( : start group 1
\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d+ : regex for date
(?: : start non capture group
(?! : negative look ahead, make sure we don't have the following
ORA-\d+ : literally "ORA-" followed by digits
) : end lookahead
(?! : negative look ahead, make sure we don't have the following
\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d+ : regex for date
) : end lookahead
. : any character
)* : non capture group is present 0 or more times
ORA-\d+ : literally "ORA-" followed by digits
(?: : start non capture group
(?! : negative look ahead, make sure we don't have the following
\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d+ : regex for date
) : end lookahead
. : any character
)* : non capture group is present 0 or more times
) : end group 1, contents in "$1"
/sg : regex delimiter, s: single line, g: global
输入文件示例:
2017-11-29 23:51:46,013 (Foo.java:67) FATAL - foo.bar()-got exception
during load of zzz javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
ORA-04031: unable to allocate 3896 bytes of shared
memory ("shared pool","selec t licensekey0_.ID as ID...","sga
heap(1,0)","kglsim object batch")
2017-11-29 23:51:46,013 (Foo.java:67) FATAL - foo.bar()-got exception
during load of zzz javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
(NO ORA.....) unable to allocate 3896 bytes of shared
memory ("shared pool","selec t licensekey0_.ID as ID...","sga
heap(1,0)","kglsim object batch")
2017-11-29 23:51:46,013 (Foo.java:67) FATAL - foo.bar()-got exception
during load of zzz javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
ORA-04031: unable to allocate 3896 bytes of shared
memory ("shared pool","selec t licensekey0_.ID as ID...","sga
heap(1,0)","kglsim object batch")
结果:
2017-11-29 23:51:46,013 (Foo.java:67) FATAL - foo.bar()-got exception
during load of zzz javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
ORA-04031: unable to allocate 3896 bytes of shared
memory ("shared pool","selec t licensekey0_.ID as ID...","sga
heap(1,0)","kglsim object batch")
2017-11-29 23:51:46,013 (Foo.java:67) FATAL - foo.bar()-got exception
during load of zzz javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
ORA-04031: unable to allocate 3896 bytes of shared
memory ("shared pool","selec t licensekey0_.ID as ID...","sga
heap(1,0)","kglsim object batch")
第二个区块不显示