mIRC Homepage
Posted By: keeker search log files - 24/04/03 10:26 PM
i am trying to make a script to search log files. What i need is a way to search a monthly log for text, which i have done with:

Code:
alias scanfile {
  var %svar $2-
   window -elk0 @test
   filter -fw $curlogfile @test *%svar*
   savebuf @test savedlogfilename.txt
   send $1 savedlogfilename.txt
   timerdelfile 1 40 remove savedlogfilename.txt
}


right now that works ok, if someone types stats send *keeker*kicked*neoknight* , i have an ontext event that runs scanfile $nick %text and it searches the log file for the specified text, but what i WANT it to do is be able to search for the text and return the part of the log that contains it, not just the line, but like 15 lines before that and 15 lines after it.

like if i was searching for if someone was kicked and they didnt think they deserved it, an SOP could retrive that part of the log file, becaue if they retrived the whole log file, its like over 3 MB right now fo the month, thats a lot to send if the SOP has a 56k modem.

was wondering if there is a way to do something like this?
Posted By: Nimue Re: search log files - 25/04/03 12:40 AM
Code:
alias scanfile2 {
  window -h @!
  filter -fw $curlogfile @! $+(*,$2-,*)
  ; check for a match, and find the line number of the last match.
  if $filtered && $read($curlogfile,w,$line(@!,$line(@!,0))) {
    ; load/save/send the range of lines
    loadbuf $+($iif($calc($readn -15) > 0,$ifmatch,1),-,$calc($readn +15)) -r @! $curlogfile
    savebuf @! $mkfn($1) $+ .scan.txt
    dcc send $1 $mkfn($1) $+ .scan.txt
  }
  window -c @!
}
on *:filesent:*.scan.txt:.remove $filename
Posted By: qwerty Re: search log files - 25/04/03 12:45 AM
Try this:
Code:
alias scanfile {
  if $read($curlogfile,nw,* $+ $$2-*) == $null { .notice $1 No matches found | return }
  var %file [color:red]=[/color] $+(savedlogfilename,$ticks,.txt) 
  filter -ffcr $iif($calc($readn -15) > 0,$ifmatch,1) $+ $calc(-15 - $readn) $curlogfile %file
  if $file(%file) { dcc send -c $1 %file }
  .timer $+ %file -oi 2 40 .remove %file
}
on *:filesent:savedlogfilename?*: .timer $+ $nopath($filename) off | .remove $+(",$filename,")


The key feature here is /filter's -r switch, which allows you to define a line range in the input file. Notice that you don't need the @window. Also, imo, you should make it a habit of using "=" in /var, otherwise you'll run into problems sooner or later.

If you want to increase the line range, change both instances of 15 to a higher number. If you are worried about the size of the output file, consider compressing the file prior to sending it, perhaps with Necroman's ngzipn.dll (you can find it at the mircscripts.org DLL section), and have the receiver's script decompress it. Alternatively, you could use winzip/winrar's commandline tools for this.
Posted By: qwerty Re: search log files - 25/04/03 12:50 AM
Ah, you already replied by the time I hit "Send". Your way, that grabs the last match instead of the first makes more sense too smile

There's a little typo in ...$line(@!,$line(@!,0)1)
Posted By: Nimue Re: search log files - 25/04/03 01:01 AM
Thanks for pointing out that typo smile
Your way is definatley faster, I forgot all about the -r switch for /filter laugh
Posted By: keeker Re: search log files - 25/04/03 02:37 AM
thanks qwerty, its working pretty good now. smile grin
© mIRC Discussion Forums