mIRC Homepage
Posted By: bodo0815 read last 10 lines from txt - 07/05/05 02:19 PM
hello...

need a script thats read the last 10 lines from a txt-file

thx Bodo
Posted By: FiberOPtics Re: read last 10 lines from txt - 07/05/05 02:53 PM
/help /play

Quote:

The -f# switch playes the whole file starting from the specified line.

/play -f9 moo.txt


10 last lines is $calc($lines(moo.txt) - 9) so //play -sef $+ $calc($lines(moo.txt) - 9) moo.txt would play it to your status window.

Other possibilities if you don't want to /play the contents but just want to have them somewhere:

window -n @lines
loadbuf 10 @lines moo.txt

So now the window @lines contains the last 10 lines of moo.txt, you could loop through them now with $line(@lines,N)

You could also loop through the file, by using $read or the file handling commands.

All kind of depends on what you want to do.
Posted By: SladeKraven Re: read last 10 lines from txt - 07/05/05 03:04 PM
Try something along the lines of...

Code:
alias read {
  var %x = $calc($lines(file.txt) - 9)
  var %lines = $lines(file.txt)
  while (%x <= %lines) {
    echo -a $read(file.txt,%x)
    inc %x
  }
}


All the best,

-Andy
Posted By: SladeKraven Re: read last 10 lines from txt - 07/05/05 03:05 PM
I was too slow, and plus I didn't think of the /play command.

Oh, shucks!

-Andy
Posted By: MikeChat Re: read last 10 lines from txt - 07/05/05 03:32 PM
im not sure what you are going to do, play to a channel/nick?

if you want to read it but not display it to others try

alias readtext {
window @read
loadbuf 10 -p @read text.txt
}
Posted By: bodo0815 Re: read last 10 lines from txt - 07/05/05 04:10 PM
@SladeKraven it works fine...thx

how can i display the last 10 lines with a specific word?
sample-txt:
blaaa nico holla
blaaa sven holla
blaaa nico holla
blaaa sven holla
blaaa nico holla
blaaa sven holla
.
.
... i will only lines with "nico"

thx Bodo
Posted By: SladeKraven Re: read last 10 lines from txt - 07/05/05 04:24 PM
Do you mean like this?

Code:
alias read {
  var %lines = $lines(file.txt)
  var %x = $calc($lines(file.txt) - 9)
  while (%x <= %lines) {
    if (*nico* iswm $read(file.txt,%x)) echo -a $v2
    inc %x
  }
}


-Andy
Posted By: bodo0815 Re: read last 10 lines from txt - 07/05/05 04:49 PM
yes, but this give me only one match
i use a trigger with your alias like !search*
how can i use this: if (*nico* iswm... to if $2 iswm...?

thx bodo ...sorry for english, i am from german :-)
Posted By: SladeKraven Re: read last 10 lines from txt - 07/05/05 05:05 PM
It doesn't give you one match, with the following...

blaaa nico holla
blaaa sven holla
blaaa nico holla
blaaa sven holla
blaaa nico holla
blaaa sven holla

It'll echo "blaaa nico holla" 3 times.

As for the trigger..

Code:
On *:Text:!trigger *:#: {
  var %lines = $lines(file.txt)
  var %x = $calc($lines(file.txt) - 9)
  while (%x <= %lines) {
    if ($+(*,$2,*) iswm $read(file.txt,%x)) msg $chan $v2
    inc %x
  }
}


All the best,

-Andy
Posted By: bodo0815 Re: read last 10 lines from txt - 07/05/05 05:30 PM
ahhh... this search the matches on the last 10 entries?
hmmm... i will the last 10 entries with a specific word form a whole txt :-)

thx bodo
Posted By: SladeKraven Re: read last 10 lines from txt - 07/05/05 05:32 PM
Yes, using the last 10 lines of the file.txt, it matches all the lines with $2 in it.

-Andy
Posted By: bodo0815 Re: read last 10 lines from txt - 07/05/05 05:43 PM
sorry... but how can i get the last 10 entries from a whole txt with a specific word?

thx bodo
Posted By: MikeChat Re: read last 10 lines from txt - 07/05/05 07:44 PM
try this
Code:
alias last10 {
var %lines = $lines(filename),  %i
while (%i <= 10) {
if (nico isin $read(filename,%lines) { msg $active  $read(filename,%lines) | inc %i  }
dec %lines
}


That will be in reverse order but will be the last 10 entries that have nico in the line
Posted By: SladeKraven Re: read last 10 lines from txt - 07/05/05 07:50 PM
Missing a closing } brace, dude. smile

Code:
alias last10 {
  var %lines = $lines(filename),  %i
  while (%i <= 10) {
    if (nico isin $read(filename,%lines) { msg $active  $read(filename,%lines) | inc %i  }
    dec %lines
  }
[color:red]}[/color]
Posted By: FiberOPtics Re: read last 10 lines from txt - 07/05/05 07:54 PM
It's quite inefficient to loop through each line and check for a match, why don't you guys just use /filter?

filter -ffc file.txt tmp *nico*

The file "tmp" now contains all lines which contain the word nico, and you can now do with it whatever you want, whether that be /play, /loadbuf, loop through it, whatever.
Posted By: MikeChat Re: read last 10 lines from txt - 08/05/05 09:23 PM
FiberOptics is right, I tested the way I was doing it vs the /filter and the filter is a lot faster despite reading the whole file (I chose a log with over 600,000 lines for testing)
here are two output choices:
Code:
alias l10 {
  var %filename = $sfile($logdir*.txt)
  filter -ffc %filename tmp *nico* 
  window @read
  loadbuf 10 -p @read tmp
  .remove tmp
}
alias last10 {
  var %filename = $sfile($logdir*.txt)
  filter -ffc %filename tmp *nico* 
  var %lines = $lines(tmp)
  echo -a . %lines
  var %10line = $calc(%lines - 10)
  while (%10line <= %lines) { 
    if ($read(tmp,%10line)) { msg $active $read(tmp,%10line) }  
    inc %10line
  }
  .remove tmp
}


Some things to consider:
are there spaces in the path to the file you want to read from, if so you will need to use "" around the path\filename
if you have only one file you want to read from, you will want to specify that path\filename instead of the $sfile()

Andy, you are right I missed the closing "}" sadly I also missed a closing ")" on the while loop.... dude smile

My bad for using the msg board reply window to script in, rather than the mIRC remote editor
Posted By: FiberOPtics Re: read last 10 lines from txt - 08/05/05 09:29 PM
Btw I think some people have misinterpreted his request.

He stated:

Quote:
sorry... but how can i get the last 10 entries from a whole txt with a specific word?

Yet people are taking the last ten lines, looping through them and looking for occurence of nico.

That is not the last ten entries with a specific word.

Suppose nico is in 15 lines in the file, and 3 of them are in the last 10 lines. With their method, you'd get just 3, while he stated he wants the last 10 entries with that word. You have to loop through the entire file starting from the end to the beginning, and stop when you have 10 or eventually when you reach the beginning.

In this case, filter is even much much preferable over $read.

One could also use the file handling commands, which are also very quick.
Posted By: MikeChat Re: read last 10 lines from txt - 08/05/05 10:18 PM
In my first run at this my attempt did select the last 10 lines that had the matchtext, not the last 10 lines. My test of that method and my test of the /filter method matched content (my first attempt was in reverse order in the output though)

also I could't get mIRC to use /play with the output from filter going to "tmp", so I used tmp.txt and it works fine, any idea why that is?
Code:
alias last10.2 {
  var %filename = $sfile($logdir*.log)
  filter -ffc %filename tmp.txt *nico* 
  var %lines = $lines(tmp.txt)
  echo -a . %lines
  var %10line = $calc(%lines - 10)
  .play -f $+ %10line tmp.txt
  .timerlast10 1 12 .remove tmp.txt
}


cheers
Posted By: DaveC Re: read last 10 lines from txt - 09/05/05 12:30 AM
I Thought I might make mention as to WHY filter is alot lot faster.

/filter well pass through the file ONCE filtering the lines into the result destination (file/window/etc)

each $read() well read from the front of the file upto the line selected, so just to get the number of lines takes an entire read of the file (excluding files with number of lines on first line)
So say a 10 line files size is stored in %i (read 10 lines)
then read line number %i (10) (read 20 lines), then dec %i
then read line number %i (9) (read 29 lines), then dec %i
then read line number %i (8) (read 37 lines), then dec %i
then (7) 44 (6) 50 (5) 55 (4) 59 (3) 62 (2) 64 (1) 65 lines read in total
The number of total reads of the file can normally be worked out as about 1 total read for every 2 lines, thats 250,000 complete readings of the file on a 500,000 line file verses 1

* fread doesnt have this problem of course.
Posted By: MikeChat Re: read last 10 lines from txt - 09/05/05 06:58 PM
I didn't know $read(file,N) still read through looking when you supply the line number...
Anyway I had not used fread before and so I tested it with this
Code:
alias readfile {
  var %file = $sfile($logdir*.log)
  var %parm = $$?="Enter word(s) to find"
  .fopen logs %file
  echo -a $fopen(logs).fname
  echo -a errors: $ferr
  :findnext
  .fseek -w logs $+(*, %parm ,*)
  if ($ferr > 0) { goto end }
  if ($feof > 0) { goto end }
  elseif ($ferr <= 0) { echo -a $fread(logs) | goto findnext }
  :end
  fclose logs
}


It is also fast but maybe more awkward than the /filter

any pointers on using the fread file handling?
© mIRC Discussion Forums