I got bored, I am a MUD programmer in my spare time and part of the job includes checking logs. These logs are usually very very long text files so I wrote a little app to do the job for me. Yes you can use find to do almost the same job and yes UNIX grep too but mine has 1 neat feature that the others don't have, whilst it does not support regular expressions what you can do is search for a pattern in a file with an optional exclusion pattern so you can do:
grep /f c:\documents and settings\logfile.txt /p Luke /e logout
which will fetch all lines containing the pattern "Luke" which do not contain the pattern "logout"
here's the (ropey) code:
EULA
Take it, use it modify it, give it away, but if you make it use reg-exp's then I want a copy. Theres nothing in it a newbie can't cope with so if you must claim it as your own then do so with my pity ;)
- Luke
grep /f c:\documents and settings\logfile.txt /p Luke /e logout
which will fetch all lines containing the pattern "Luke" which do not contain the pattern "logout"
here's the (ropey) code:
;grep /f c:\path\to\file /p pattern [/e exclude] Type result Field info$ End Type Graphics 640,480,16,2 a$=CommandLine() If a$= "" Then quit_program("Usage grep /f filename /p pattern [/e exclude_pattern]") End If Global pattern$ Global exclude$ Global file$ parseline(a$) infile=ReadFile (file) If Not infile Then quit_program("File: "+file+" not found") counter=0 Global count=0 srtbusy$="" SetBuffer BackBuffer() While Not Eof(infile) Cls Text 15,15, "Searching "+file Text 15,35, "For lines containing :"+pattern+":" If exlcude<>"" Then Text 15,55, "Excluding lines containing "+exclude counter=counter+1 l$=ReadLine(infile) If counter Mod(5)=0 strbusy$=strbusy+"." If Instr(l,pattern) If exclude="" Then add_result(l) Else If Not Instr(l,exclude) Then add_result(l) End If End If Text 15,75,""+counter+" lines processed." Text 15,95,""+count+" occurences found." Text 15,115,strbusy If strbusy="...................." strbusy="" If KeyHit(1) Then quit_program("User Aborted!") Flip(1) Wend If count Then outfile =WriteFile ("grep_resutlts.txt") For n.result=Each result WriteLine (outfile, n\info) Next Text 15,135,"Done" Text 15,155,"Results written to: "+CurrentDir()+"grep_results.txt" Text 15,175,"Press any key to continue." Else Text 15,135,"No Matches Found" Text 15,155,"Press any key to continue." End If Flip(1) WaitKey End Function parseline(the_line$) idxf= Instr(the_line,"/f") If Left(the_line,2)<>"/f" quit_program("Usage grep /f filename /p pattern [/e exclude_pattern]") If Not idxf Then quit_program("Usage grep /f filename /p pattern [/e exclude_pattern]") idxp=Instr(the_line,"/p") If Not idxp Then quit_program("Usage grep /f filename /p pattern [/e exclude_pattern]") idxe=Instr(the_line,"/e") If (idxf>idxp) Then quit_program("Usage grep /f filename /p pattern [/e exclude_pattern]") If (idxe>0) And (idxe<idxp) Then quit_program("Usage grep /f filename /p pattern [/e exclude_pattern]") file=Mid(the_line,idxf+3,idxp-5) If idxe Then pattern=Mid(the_line,idxp+3,idxe-idxp-4) exclude=Right(the_line,Len(the_line)-idxe-2) Else pattern=Right(the_line,Len(the_line)-idxp-2) End If End Function Function add_result(l$) count=count+1 r.result=New result r\info=l End Function Function quit_program(message$) Print message Print "Press any key." WaitKey End End Function
EULA
Take it, use it modify it, give it away, but if you make it use reg-exp's then I want a copy. Theres nothing in it a newbie can't cope with so if you must claim it as your own then do so with my pity ;)
- Luke