2010-02-02 18 views
5

Quiero ser capaz de utilizar un 'grep' o '-M pcregrep' como solución que analiza un archivo de registro que se ajuste a los siguientes parámetros:análisis de un archivo de registro de longitud variable multilínea

  • Cada entrada del registro puede haber múltiples líneas de longitud
  • Primera línea de entrada de registro tiene la llave que quiero buscar
  • aparece Cada tecla en más de una línea de

Así, en el ejemplo a continuación me gustaría volver cada línea que h como KEY1 en él y todas las líneas de apoyo debajo de él hasta el siguiente mensaje de registro.

 
Log file: 
01 Feb 2010 - 10:39:01.755, DEBUG - KEY1:randomtext 
     blah 
     blah2 T 
     blah3 T 
     blah4 F 
     blah5 F 
     blah6 
     blah7 
01 Feb 2010 - 10:39:01.757, DEBUG - KEY1:somethngelse 
01 Feb 2010 - 10:39:01.758, DEBUG - KEY2:randomtest 
this is a test 
01 Feb 2010 - 10:39:01.760, DEBUG - KEY1:more logs here 
01 Feb 2010 - 10:39:01.762, DEBUG - KEY1:eve more here 
this is another multiline log entry 
keeps on going 
but not as long as before 
01 Feb 2010 - 10:39:01.763, DEBUG - KEY2:testing 
test test test 
end of key2 
01 Feb 2010 - 10:39:01.762, DEBUG - KEY1:but key 1 is still going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
okay enough 
01 Feb 2010 - 10:39:01.762, DEBUG - KEY3:and so on 
and on 
 
Desired output of searching for KEY1: 
01 Feb 2010 - 10:39:01.755, DEBUG - KEY1:randomtext 
     blah 
     blah2 T 
     blah3 T 
     blah4 F 
     blah5 F 
     blah6 
     blah7 
01 Feb 2010 - 10:39:01.757, DEBUG - KEY1:somethngelse 

01 Feb 2010 - 10:39:01.760, DEBUG - KEY1:more logs here 
01 Feb 2010 - 10:39:01.762, DEBUG - KEY1:eve more here 
this is another multiline log entry 
keeps on going 
but not as long as before 
01 Feb 2010 - 10:39:01.762, DEBUG - KEY1:but key 1 is still going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
okay enough 

yo estaba tratando de hacer algo como: '(. * \ N) TECLA1 +'
pcregrep -M archivo de registro
pero definitivamente no funciona bien.

+0

¿Qué define el final de una entrada? ¿Se garantiza que las líneas dentro de una entrada no comenzarán con un dígito, sino que una línea que define una nueva entrada lo hará? –

+0

Esto podría ser más fácil utilizando un pequeño script en lugar de una expresión regular. ¿Alguna razón para no hacer eso? –

Respuesta

-1

Añadiendo a la contestación de ghostdog74 (muchas gracias por cierto, funciona muy bien)

Ahora toma la entrada de línea de comandos en forma de "./parse clave de archivo" y maneja los niveles de registro de ERROR, así como DEPURAR

 
#!/bin/bash 
awk -vkey="$2" ' 
$0~/DEBUG|ERROR/ && $0 !~key{f=0} 
$0~key{ f=1 } 
f{print} ' $1 
+2

así que considere aceptar la respuesta y podría publicar esto juntos en su pregunta en su lugar – ghostdog74

+0

Lo haría pero dice que no puedo aceptar mi propia respuesta durante 2 días – Urgo

+0

Urgo, esta publicación solo ajusta la respuesta por ghostdog74. Debe marcar ghostdog74 como respuesta y editar su pregunta original para agregar este ajuste. – adam

7

si está en * nix, puede utilizar la cáscara

#!/bin/bash 
read -p "Enter key: " key 
awk -vkey="$key" ' 
$0~/DEBUG/ && $0 !~key{f=0} 
$0~key{ f=1 } 
f{print} ' file 

salida

$ cat file 
01 Feb 2010 - 10:39:01.755, DEBUG - KEY1:randomtext 
     blah          
     blah2 T          
     blah3 T          
     blah4 F          
     blah5 F          
     blah6          
     blah7          
01 Feb 2010 - 10:39:01.757, DEBUG - KEY1:somethngelse 
01 Feb 2010 - 10:39:01.758, DEBUG - KEY2:randomtest 
this is a test          
01 Feb 2010 - 10:39:01.760, DEBUG - KEY1:more logs here 
01 Feb 2010 - 10:39:01.762, DEBUG - KEY1:eve more here 
this is another multiline log entry      
keeps on going           
but not as long as before        
01 Feb 2010 - 10:39:01.763, DEBUG - KEY2:testing  
test test test           
end of key2            
01 Feb 2010 - 10:39:01.762, DEBUG - KEY1:but key 1 is still going 
and going               
and going               
and going               
and going               
and going               
and going               
and going               
and going               
and going               
and going 
and going 
and going 
okay enough 
01 Feb 2010 - 10:39:01.762, DEBUG - KEY3:and so on 
and on 

$ ./shell.sh 
Enter key: KEY1 
01 Feb 2010 - 10:39:01.755, DEBUG - KEY1:randomtext 
     blah 
     blah2 T 
     blah3 T 
     blah4 F 
     blah5 F 
     blah6 
     blah7 
01 Feb 2010 - 10:39:01.757, DEBUG - KEY1:somethngelse 
01 Feb 2010 - 10:39:01.760, DEBUG - KEY1:more logs here 
01 Feb 2010 - 10:39:01.762, DEBUG - KEY1:eve more here 
this is another multiline log entry 
keeps on going 
but not as long as before 
01 Feb 2010 - 10:39:01.762, DEBUG - KEY1:but key 1 is still going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
and going 
okay enough 
0

Tuve un requisito similar y decidí codificar una pequeña herramienta (en .net) que analiza los archivos de registro y escribe el resultado en la salida estándar.

Quizás le sea útil. Funciona con Windows y Linux (Mono)

Ver aquí: https://github.com/iohn2000/ParLog

Una herramienta para filtrar los archivos de entradas del registro que contienen una específica (expresiones regulares) patrón de registro. Funciona también con entradas de registro de líneas múltiples. por ej .: mostrar solo las entradas de registro de una determinada instancia de flujo de trabajo. Escribe el resultado en salida estándar. Use '>' para redirigir a un fichero

predeterminado startPattern es:

^[0-9]{2} [\w]{3} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3} 

esto se traduce en formato de fecha: por ejemplo: 04 Feb 2017 15: 02: 50,778

Los parámetros son:

f:wildcard  a file name or wildcard for multiple files 
p:pattern  the regex pattern to filter the file(s) 
s:startPattern regex pattern to define when a new log entry starts 

Ejemplo:

ParLog.exe -f=*.log -p=findMe 
Cuestiones relacionadas