grep regex para incluir o registro de data e hora do registro

1

Estou usando muitos arquivos de log para determinados erros relacionados ao Oracle com códigos de erro ORA-xxxxx. Todas as entradas de log começam com um registro de data e hora (por exemplo: 2017-11-29 23: 51: 46,372). Algumas das entradas de log são multilinha - como aquelas com java Exception stack traces, onde os códigos ORA-xxxxx estão bem abaixo da linha de entrada de log com o Timestamp.

Qual é o regex para encontrar o registro de data e hora do registro até o código ORA-xxxxx?

Uma vez que eu obtenho o regex acima trabalhando, o próximo é classificá-los pela data para encontrar a última vez que um determinado erro ORA-xxxxx ocorreu.

O comando P.S: grep ou perl regex serve.

Obrigado,

Exemplo de um arquivo de log

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")
    
por anjanbacchu 19.12.2017 / 08:42

1 resposta

1

Este forro perl faz o trabalho:

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

Isso imprimirá o conteúdo do grupo 1 ( $1 ) toda vez que ele for encontrado.

Opções:

-0777   : slurp mode
-n      : add a loop around the script
-E      : enable features ("say" in this case)

Regex:

/                                           : 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

Exemplo de arquivo de entrada:

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

Resultado:

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

O segundo bloco não é exibido

    
por 19.12.2017 / 11:24

Tags