mIRC Home    About    Download    Register    News    Help

Print Thread
read last 10 lines from txt #119582 07/05/05 02:19 PM
Joined: Apr 2005
Posts: 72
B
bodo0815 Offline OP
Babel fish
OP Offline
Babel fish
B
Joined: Apr 2005
Posts: 72
hello...

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

thx Bodo

Re: read last 10 lines from txt #119583 07/05/05 02:53 PM
Joined: Feb 2004
Posts: 2,019
FiberOPtics Offline
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
/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.


Gone.
Re: read last 10 lines from txt #119584 07/05/05 03:04 PM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
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

Re: read last 10 lines from txt #119585 07/05/05 03:05 PM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
I was too slow, and plus I didn't think of the /play command.

Oh, shucks!

-Andy

Re: read last 10 lines from txt #119586 07/05/05 03:32 PM
Joined: Dec 2002
Posts: 1,245
M
MikeChat Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Dec 2002
Posts: 1,245
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
}

Re: read last 10 lines from txt #119587 07/05/05 04:10 PM
Joined: Apr 2005
Posts: 72
B
bodo0815 Offline OP
Babel fish
OP Offline
Babel fish
B
Joined: Apr 2005
Posts: 72
@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

Re: read last 10 lines from txt #119588 07/05/05 04:24 PM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
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

Re: read last 10 lines from txt #119589 07/05/05 04:49 PM
Joined: Apr 2005
Posts: 72
B
bodo0815 Offline OP
Babel fish
OP Offline
Babel fish
B
Joined: Apr 2005
Posts: 72
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 :-)

Re: read last 10 lines from txt #119590 07/05/05 05:05 PM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
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

Re: read last 10 lines from txt #119591 07/05/05 05:30 PM
Joined: Apr 2005
Posts: 72
B
bodo0815 Offline OP
Babel fish
OP Offline
Babel fish
B
Joined: Apr 2005
Posts: 72
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

Re: read last 10 lines from txt #119592 07/05/05 05:32 PM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
Yes, using the last 10 lines of the file.txt, it matches all the lines with $2 in it.

-Andy

Re: read last 10 lines from txt #119593 07/05/05 05:43 PM
Joined: Apr 2005
Posts: 72
B
bodo0815 Offline OP
Babel fish
OP Offline
Babel fish
B
Joined: Apr 2005
Posts: 72
sorry... but how can i get the last 10 entries from a whole txt with a specific word?

thx bodo

Re: read last 10 lines from txt #119594 07/05/05 07:44 PM
Joined: Dec 2002
Posts: 1,245
M
MikeChat Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Dec 2002
Posts: 1,245
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

Re: read last 10 lines from txt #119595 07/05/05 07:50 PM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
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]

Re: read last 10 lines from txt #119596 07/05/05 07:54 PM
Joined: Feb 2004
Posts: 2,019
FiberOPtics Offline
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
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.


Gone.
Re: read last 10 lines from txt #119597 08/05/05 09:23 PM
Joined: Dec 2002
Posts: 1,245
M
MikeChat Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Dec 2002
Posts: 1,245
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

Re: read last 10 lines from txt #119598 08/05/05 09:29 PM
Joined: Feb 2004
Posts: 2,019
FiberOPtics Offline
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2004
Posts: 2,019
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.


Gone.
Re: read last 10 lines from txt #119599 08/05/05 10:18 PM
Joined: Dec 2002
Posts: 1,245
M
MikeChat Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Dec 2002
Posts: 1,245
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

Re: read last 10 lines from txt #119600 09/05/05 12:30 AM
Joined: Sep 2003
Posts: 4,230
D
DaveC Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
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.

Re: read last 10 lines from txt #119601 09/05/05 06:58 PM
Joined: Dec 2002
Posts: 1,245
M
MikeChat Offline
Hoopy frood
Offline
Hoopy frood
M
Joined: Dec 2002
Posts: 1,245
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?