2010-02-02 13 views
5

Ich möchte ein ‚grep‘ nutzen zu können, oder ‚pcregrep -M‘ wie-Lösung, die eine Protokolldatei analysiert, die die folgenden Parameter passend für:Parsing ein mehrzeiliges variabler Länge Protokolldatei

  • Jeder Protokolleintrag können mehrere Zeilen lang sein
  • Erste Zeile des Protokolleintrag hat den Schlüssel, den ich für
  • Jede Taste auf mehr als eine Zeile suchen möchten
  • erscheint

So im Beispiel unten ich möchte zurückkehren jede Zeile, die h als KEY1 darauf und alle unterstützenden Zeilen darunter bis zur nächsten Log-Nachricht.

 
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 

Ich habe versucht, wie etwas zu tun ist: '(. * \ N) KEY1 +'
pcregrep -M Logfile
aber definitiv nicht richtig funktioniert.

+0

Was definiert das Ende eines Eintrags? Ist sichergestellt, dass die Zeilen innerhalb eines Eintrags nicht mit einer Ziffer beginnen, sondern eine Zeile, die einen neuen Eintrag definiert? –

+0

Dies könnte einfacher sein, wenn Sie ein kleines Skript anstelle einer Regex verwenden. Irgendein Grund, das nicht zu tun? –

Antwort

-1

Hinzufügen auf Antwort auf ghostdog74 der (vielen Dank btw, es funktioniert gut)

Nimmt nun Befehlszeile-Eingabe in Form von "./parse Dateischlüssel" und behandelt Loglevel von FEHLER sowie DEBUG

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

so in Betracht ziehen, die Antwort zu akzeptieren, und Sie könnten dies zusammen in Ihrer Frage stattdessen posten – ghostdog74

+0

Ich würde aber es sagt, dass ich meine eigene Antwort für 2 Tage nicht akzeptieren kann – Urgo

+0

Urgo, dieser Beitrag optimiert nur die Antwort von ghostdog74. Sie sollten ghostdog74 als Antwort markieren und Ihre ursprüngliche Frage bearbeiten, um diese Optimierung hinzuzufügen. – adam

7

, wenn Sie auf * nix sind, können Sie die Shell verwenden

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

Ausgang

$ 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

Ich hatte eine ähnliche Anforderung und beschloss, ein kleines Tool (in .net) zu programmieren, das Protokolldateien für mich analysiert und das Ergebnis in die Standardausgabe schreibt.

Vielleicht finden Sie es nützlich. Arbeiten unter Windows und Linux (Mono)

Siehe hier: https://github.com/iohn2000/ParLog

Ein Tool-Dateien für Protokolleinträge log filtern, die eine bestimmte (regex) Muster enthalten. Funktioniert auch mit mehrzeiligen Protokolleinträgen. Beispiel: Nur Protokolleinträge von einer bestimmten Workflow-Instanz anzeigen. Schreibt das Ergebnis in die Standardausgabe. Verwenden Sie '>' in eine Datei zu umleiten

Standardstartpattern ist:

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

dies Datumsformat entspricht: zB: 4. Februar 2017 15: 02: 50.778

Parameter sind:

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 

Beispiel:

ParLog.exe -f=*.log -p=findMe 
Verwandte Themen