2010-02-02 15 views
5

bir satırlı değişken uzunlukta log dosyası ayrıştırma:Ben aşağıdaki parametreleri uyan bir günlük dosyası ayrıştırır bir 'grep' ya çözüm gibi 'pcregrep -M' yararlanmak isterler

  • Her günlük girdisi uzunluğunda birden çok satır olabilir
  • günlük girdisinin
  • Birinci satır Ben Her anahtar dönmek isteyeyim aşağıdaki örnekte

Yani daha sonra bir satırda görünüyor

  • aramak istediğiniz anahtar vardır her satır h Bunun üzerine KEY1 ve bir sonraki günlük mesaja kadar altındaki destek çizgileri.
     
    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 
    

    ben böyle bir şey yapmaya çalışıyordum: '(. * \ N) key1 +'
    pcregrep -M kesinlikle logfile'a
    ama doğru çalışmıyor.

  • +0

    Bir girişin sonunu ne tanımlar? Bir girişteki satırların bir rakamla başlayamayacağı garanti edilir, ancak yeni bir girişi tanımlayan bir satır olur mu? –

    +0

    Bu, normal ifadelerden ziyade küçük bir betik kullanılarak daha kolay olabilir. Bunu yapmamanın herhangi bir sebebi var mı? –

    cevap

    -1

    ghostdog74 cevabı üzerine ekleme kullanabilirsiniz

    (çok btw teşekkür ederim, harika çalışır)

    Şimdi komut satırı girişini "./parse dosya anahtarı" biçiminde alır ve ERROR'un loglevellerini ve ayrıca DEBUG

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

    bu yüzden cevabı kabul etmeyi düşünün ve bu soruyu – ghostdog74

    +0

    yerine yazabilirim ama ben 2 günlük – Urgo

    +0

    Urgo için kendi cevabımı kabul edemediğimi söylüyorum, bu mesaj sadece ghostdog74 ile cevabı değiştiriyor. Ghostdog74'ü yanıt olarak işaretlemeli ve bu tweak'i eklemek için orijinal sorunuzu düzenlemelisiniz. – adam

    7

    sen * nix üzerinde iseniz, kabuk

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

    çıkış

    $ 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

    Benzer bir gereksinim vardı ve günlük dosyalarını benim için ayrıştıran ve sonucu standart çıktıya yazan küçük bir aracı (.net) kodlamaya karar verdim.

    Belki de yararlı buluyorsunuz. Windows ve Linux (Mono) üzerine İşleri

    Buraya bakın: https://github.com/iohn2000/ParLog

    bir araç (regex) desenin bir özgü içeren günlük girişleri için günlük dosyaları filtrelemek için. Çok satırlı günlük girişleriyle de çalışır. örn .: yalnızca belirli bir iş akışı örneğinden kayıt girişlerini göster. Sonuç, standart çıktıya yazar. Bir dosya

    varsayılan startPattern içine yönlendirmek için '>' kullanın: Örneğin: 04 Feb 15 2017: 02: 50.778

    Parametreler gibidir:

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

    bu tarih formatı tekabül

    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 
    

    Örnek:

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