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?
wie oft kommt ein String im Log vor ?
-
- Posts: 213
- Joined: 2007-01-13 19:58
Re: wie oft kommt ein String im Log vor ?
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
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.
-
- Administrator
- Posts: 2641
- Joined: 2004-01-21 17:44
Re: wie oft kommt ein String im Log vor ?
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
-
- Posts: 5923
- Joined: 2004-05-23 12:53
Re: wie oft kommt ein String im Log vor ?
Warum nicht einfach `grep -c $SEARCH_STRING /path/to/file`?
Logdateien sind i. d. R. zeilenorientiert, so dass das problemlos funktionieren sollte.
Logdateien sind i. d. R. zeilenorientiert, so dass das problemlos funktionieren sollte.
-
- Administrator
- Posts: 2641
- Joined: 2004-01-21 17:44
Re: wie oft kommt ein String im Log vor ?
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:
A propos:
Dafür gibt's den useless use of cat Award - den hab ich schon lange nicht mehr vergeben :Dmatzewe01 wrote:Code: Select all
cat <dateiname> | grep OSLO12345.jpg | wc -l
“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
-
- Administrator
- Posts: 2641
- Joined: 2004-01-21 17:44
Re: wie oft kommt ein String im Log vor ?
So, hier noch eine Lösung für vollschmerzbefreite Python-Fanatiker (auszuführen im interaktiven Python-Interpreter oder alternativ bpython):
Alternativ einfach das angehängte Skript runterladen (da steht die Langfassung drin), nach /usr/local/bin/ecount kopieren, chmod 0755 drauf und dann einfachloslassen (und hoffen, dass es keine Exception gibt
)
Code: Select all
fp = open("/pfad/zu/date", "r")
content = fp.read()
import re
len(re.findall(r"pattern", content, re.MULTILINE))
fp.close()
Code: Select all
ecount "meinemuster\.jpg" /var/log/meinlog
“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