wie oft kommt ein String im Log vor ?

Alles was in keine andere Systemkategorie passt
amiga1200
Posts: 213
Joined: 2007-01-13 19:58
 

wie oft kommt ein String im Log vor ?

Post by amiga1200 »

ich habe eine Logdaei, die ca. 5 GB groß ist.
Nun will ich wissen, wie oft sich dort ein bestimmtes Bild (z.B. OSLO12345.jpg) wieder finde.
Ich kann mir zwar ein VB/PHP Tool schreiben, aber das geht bestimmt auch
unter der Shell einfacher?
amiga1200
Posts: 213
Joined: 2007-01-13 19:58
 

Re: wie oft kommt ein String im Log vor ?

Post by amiga1200 »

ich vermute, der String kommt ca.200 mal vor,
da brauche ich schon die genaue Anzahl.

Dann werde ich mir doch ein Script in PHP schreiben müssen.

oder hat vielleicht hier zufällig jemand ein Shell-Script,
was sowas macht?
Mit Shell bin ich noch etwas auf dem Kriegsfuss
Last edited by amiga1200 on 2010-11-30 11:09, edited 1 time in total.
User avatar
daemotron
Administrator
Administrator
Posts: 2641
Joined: 2004-01-21 17:44
 

Re: wie oft kommt ein String im Log vor ?

Post by daemotron »

Ungetestet:

Code: Select all

awk 'BEGIN {ausdruck="gesuchter string"; vk=0; tmp=""; i=0} {tmp=$0; i=index(tmp, ausdruck); while(i>0){vk++;tmp=substr(tmp,i,length(tmp)-i+1);i=index(tmp, ausdruck)}} END {print vk}' /pfad/zu/datei
“Some humans would do anything to see if it was possible to do it. If you put a large switch in some cave somewhere, with a sign on it saying 'End-of-the-World Switch. PLEASE DO NOT TOUCH', the paint wouldn't even have time to dry.” — Terry Pratchett, Thief of Time
Roger Wilco
Posts: 5923
Joined: 2004-05-23 12:53
 

Re: wie oft kommt ein String im Log vor ?

Post by Roger Wilco »

Warum nicht einfach `grep -c $SEARCH_STRING /path/to/file`?

Logdateien sind i. d. R. zeilenorientiert, so dass das problemlos funktionieren sollte.
User avatar
daemotron
Administrator
Administrator
Posts: 2641
Joined: 2004-01-21 17:44
 

Re: wie oft kommt ein String im Log vor ?

Post by daemotron »

grep -c gibt nur die Anzahl der Zeilen aus, in denen der gesuchte String vorkommt. Dem OP reicht diese Angabe jedoch nicht - so habe ich seine Antwort auf matzewe01's Vorschlag jedenfalls interpretiert. Daher die kranke Verrenkung mit awk (mit Perl wäre das wohl einfacher gewesen...) :-?

A propos:
matzewe01 wrote:

Code: Select all

cat <dateiname> | grep OSLO12345.jpg | wc -l
Dafür gibt's den useless use of cat Award - den hab ich schon lange nicht mehr vergeben :D
“Some humans would do anything to see if it was possible to do it. If you put a large switch in some cave somewhere, with a sign on it saying 'End-of-the-World Switch. PLEASE DO NOT TOUCH', the paint wouldn't even have time to dry.” — Terry Pratchett, Thief of Time
User avatar
daemotron
Administrator
Administrator
Posts: 2641
Joined: 2004-01-21 17:44
 

Re: wie oft kommt ein String im Log vor ?

Post by daemotron »

So, hier noch eine Lösung für vollschmerzbefreite Python-Fanatiker (auszuführen im interaktiven Python-Interpreter oder alternativ bpython):

Code: Select all

fp = open("/pfad/zu/date", "r")
content = fp.read()
import re
len(re.findall(r"pattern", content, re.MULTILINE))
fp.close()
Alternativ einfach das angehängte Skript runterladen (da steht die Langfassung drin), nach /usr/local/bin/ecount kopieren, chmod 0755 drauf und dann einfach

Code: Select all

ecount "meinemuster\.jpg" /var/log/meinlog
loslassen (und hoffen, dass es keine Exception gibt :wink: )
“Some humans would do anything to see if it was possible to do it. If you put a large switch in some cave somewhere, with a sign on it saying 'End-of-the-World Switch. PLEASE DO NOT TOUCH', the paint wouldn't even have time to dry.” — Terry Pratchett, Thief of Time