Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Tail: the last lines of a text file


  • Please log in to reply
18 replies to this topic
CoreTwo
  • Members
  • 33 posts
  • Last active: Dec 29 2014 03:06 PM
  • Joined: 16 Oct 2007

btw, you can write the original function more compactly

Tail(k,file) {  ; Return the last k lines of file

   Loop Read, %file%

      i := Mod(A_Index,k), L%i% := A_LoopReadLine

   Loop % k

      i := Mod(i+1,k), L .= L%i% "`n"

   Return L

}

My L array kept getting `n`n`n`n`n`n`n at the start, I suspect changes (or bugs) in AHK over the past 5 years .... so I modified it like this:

ReadTail(10,"c:\temp\logfile.txt")      ;read the last 10 lines of this file.



ReadTail(k,file)   ; Read tail file, get last K lines.
{ 
    Loop Read, %file%
    {    
        if (A_LoopReadLine)               ;If this line is not null, add it.
            i := Mod(A_Index,k), L%i% := A_LoopReadLine
    }    

    Loop % k
    {         
        i := Mod(i+1,k), HasData:= L%i%   ;calculate mod and put current line into HasData
        if (HasData)                      ;if HasData has data, add it to L
        {               
            if (L)                        ;this is not the first time to add data, so it can begin with `n
                L .= "`n" L%i%    
            if (not L)                    ;this is the first time adding to L so it should not get a `n to start with
                L .= L%i%                 
        }
    }
}

For logfile.txt I used this.

Line1
Line2
Line3
Line4
LineNEW

It was a long hard battle.  AHK is particular about the "`n" in quotes and can't make up it's mind if % are required or not... gets you both ways with L%i%.

 

 

 

 



Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

just to confirm, is the following the latest code update?:

PS: ReadTail(k,file) did not work for me. ;)

Tail(k,file) {  ; Return the last k lines of file

   Loop Read, %file%

      i := Mod(A_Index,k), L%i% := A_LoopReadLine

   Loop % k

      i := Mod(i+1,k), L .= L%i% "`n"

   Return L

}


alangn
  • Members
  • 3 posts
  • Last active: Oct 08 2015 09:46 PM
  • Joined: 14 Jul 2015

I am trying to use "Tail" for totally 8 files and each of them will check for last 3 lines in txt. however when I try to run the ahk it return a error saying duplicate function definition. for Tail(3, file)   

 

it works if I only leave it once.

 

Is the "Tail" command can not be use more than once? or is there a way to fix my code? Many thanks!!

Tail(3, file)
{
loop read, putty1.txt
{
i := Mod(A_index,3)
L%i% = %A_LoopReadLine%
    }
    L := L%i%
    Loop % 3-1
    {
      IfLess i,1, SetEnv i,%3%
      i--      ; Mod does not work here
      L := L%i% "`n" L
    }
    Return L
}
 
lines1 := % Tail(3, A_ScriptFullPath)
 
if lines1 contains %errorA%
{
errorA1 = 1
}
 
 

Tail(3, file)
{
loop read, putty2.txt
{
i := Mod(A_index,3)
L%i% = %A_LoopReadLine%
    }
    L := L%i%
    Loop % 3-1
    {
      IfLess i,1, SetEnv i,%3%
      i--      ; Mod does not work here
      L := L%i% "`n" L
    }
    Return L
}
 
lines2 := % Tail(3, A_ScriptFullPath)
 
if lines2 contains %errorA%
{
errorA2 = 1
}



space
  • Members
  • 520 posts
  • Last active:
  • Joined: 12 Aug 2014
What you've done is written the function three times. The purpose of a function is that you write it once and you call it in this case 8 times each time with different parameters, something like this
Lines1:=Tail(3, "putty1.txt")
Lines2:=Tail(3, "putty2.txt")
Lines3:=Tail(3, "putty3.txt")
; etc


Tail(3, file) ; there can only be one tail function in a script
{
loop read, %file%
{
i := Mod(A_index,3)
L%i% = %A_LoopReadLine%
    }
    L := L%i%
    Loop % 3-1
    {
      IfLess i,1, SetEnv i,%3%
      i--      ; Mod does not work here
      L := L%i% "`n" L
    }
    Return L
}
If you want to use your own script simply remove the
Tail(3, file)
{
and the close } each time so you simply execute all the code in the script because that is what in fact you've written. You may need to study what a function is before continuing http://ahkscript.org...s/Functions.htm