grep regex包含日志时间戳


1

我正在使用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")

查找日期(甚至不需要使用正则表达式),检查该“行”的数据是否包含ORA-xx
Seth

Answers:


1

这个perl one liner完成了这项工作:

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")

不显示第二个块


非常感谢。并非所有日志条目都具有相同的结构。某些条目的ORA-XXXXX与时间戳位于同一行。其他人将有3-4个换行,其他人将有更多。我可以使用1个正则表达式来满足所有条件吗?非常感谢。
anjanbacchu

@anjanbacchu:它应该适用于所有这些情况。
托托

谢谢。我接受了这个答案。现在,我已经在superuser.com/questions/1278651 / ...上询问了这个问题的第二部分。小心试试?再次感谢,
anjanbacchu

有没有办法只输出**给定特定ORA-xxxx代码的时间戳?
anjanbacchu '17年

@anjanbacchu:看看我对另一个问题的回答。
托托
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.