2010-02-02 17 views
5

मैं एक 'ग्रेप' या समाधान की तरह 'pcregrep -M' कि कि निम्नलिखित मानकों फिट बैठता है एक लॉग फ़ाइल को पार्स करता है का उपयोग करने में सक्षम होना चाहते पार्स:एक बहु चर लंबाई लॉग फ़ाइल

  • प्रत्येक लॉग प्रविष्टि लंबाई में कई पंक्तियों हो सकता है
  • लॉग प्रविष्टि की पहली पंक्ति महत्वपूर्ण यह है कि मैं
  • प्रत्येक कुंजी उदाहरण नीचे मैं वापस जाने के लिए चाहते हो जाएगा में एक से अधिक पंक्ति में दिखाई दे

तो लिए खोज करना चाहते है हर पंक्ति है कि एच इसके बारे में KEY1 और अगले लॉग संदेश तक इसके नीचे की सभी सहायक पंक्तियां।

 
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 

मैं की तरह कुछ करने के लिए कोशिश कर रहा था: '(। * \ N) कुंजी 1 +'
pcregrep -M लॉगफ़ाइल
लेकिन निश्चित रूप से सही काम नहीं करता।

+0

प्रवेश के अंत को क्या परिभाषित करता है? क्या यह गारंटी है कि किसी प्रविष्टि के भीतर की रेखाएं अंकों से शुरू नहीं होंगी, लेकिन एक नई प्रविष्टि को परिभाषित करने वाली रेखा होगी? –

+0

रेगेक्स की बजाय एक छोटी लिपि का उपयोग करना आसान हो सकता है। ऐसा करने का कोई कारण नहीं है? –

उत्तर

-1

ghostdog74 के जवाब देने के लिए पर जोड़ने से उपयोग कर सकते हैं (बहुत बहुत btw का शुक्र है, यह बहुत अच्छा काम करता है)

अब "./parse फ़ाइल कुंजी" के रूप में कमांड लाइन इनपुट लेता है और ERROR के Loglevels के साथ-साथ DEBUG

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

इसलिए उत्तर स्वीकार करने पर विचार करें और आप इसे अपने प्रश्न में – ghostdog74

+0

पर पोस्ट कर सकते हैं, लेकिन मैं कहूंगा कि मैं 2 दिनों – Urgo

+0

उर्गो के लिए अपना उत्तर स्वीकार नहीं कर सकता, यह पोस्ट केवल ghostdog74 द्वारा उत्तर को बदल देता है। आपको ghostdog74 को उत्तर के रूप में चिह्नित करना चाहिए और इस चिमटा को जोड़ने के लिए अपना मूल प्रश्न संपादित करना चाहिए। – adam

7

अगर आप * nix पर हैं, तो आप खोल

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

उत्पादन

$ 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

मेरे पास एक समान आवश्यकता थी और मैंने थोड़ा सा उपकरण (.net) कोड करने का निर्णय लिया जो मेरे लिए लॉग फाइलों को पार्स करता है और परिणाम को मानक आउटपुट में लिखता है।

शायद आपको यह उपयोगी लगता है। विंडोज और लिनक्स (मोनो) पर काम करता है

यहाँ देखें: https://github.com/iohn2000/ParLog

एक उपकरण फिल्टर करने के लिए लॉग इन करें लॉग प्रविष्टियों कि एक विशिष्ट (regex) पैटर्न होते हैं के लिए फ़ाइलें। मल्टीलाइन लॉग प्रविष्टियों के साथ भी काम करता है। उदा .: कुछ निश्चित वर्कफ़्लो उदाहरण से केवल लॉग प्रविष्टियां दिखाएं। मानक आउटपुट के परिणाम को लिखता है। '>' का प्रयोग करें एक फ़ाइल

डिफ़ॉल्ट startPattern में पुनर्निर्देशित करना होता है: उदाहरण के लिए::

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

इस दिनांक स्वरूप से मेल खाती है 04 फ़र, 2017 15: 02: 50,778

पैरामीटर रहे हैं:

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 

उदाहरण:

ParLog.exe -f=*.log -p=findMe 
संबंधित मुद्दे