Get last 50 lines

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Get last 50 lines

24 Aug 2021, 01:58

I'd like to just get the last 50 lines of various log files, what's the easiest way to ignore the first few thousand lines and just get the last 50?

Code: Select all

Loop, Read, D:\0.log
{
    Line := A_LoopReadLine
    Lines .= Lines"`n"Line ; but ignore all lines except for the last 50 !
}
Spark
Posts: 80
Joined: 04 Jan 2017, 02:22

Re: Get last 50 lines

24 Aug 2021, 02:41

Hello,

Try This

Code: Select all

FilePath := "D:\0.log"

Loop, Read,% FilePath
	Total_Line := A_Index

StartLine := Total_Line - 50

Loop, Read, % FilePath
	if (A_Index > StartLine)
		Lines .= A_LoopReadLine "`n"

MsgBox, % Lines
HTH
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Get last 50 lines

24 Aug 2021, 02:58

Code: Select all

FileRead, logLines, D:\0.log
lines := LastLines(logLines, 50)

LastLines(ByRef string, linecount, delimiter := "`n") {
    return SubStr(string, InStr(string, delimiter,, 0, linecount) + StrLen(delimiter))
}
braunbaer
Posts: 483
Joined: 22 Feb 2016, 10:49

Re: Get last 50 lines

24 Aug 2021, 03:42

Xtra wrote:
24 Aug 2021, 02:58

Code: Select all

FileRead, logLines, D:\0.log
lines := LastLines(logLines, 50)

LastLines(ByRef string, linecount, delimiter := "`n") {
    return SubStr(string, InStr(string, delimiter,, 0, linecount) + StrLen(delimiter))
}
That omits the first n lines instead of returning the last n lines...
Spark wrote:
24 Aug 2021, 02:41
Hello,

Try This

Code: Select all

FilePath := "D:\0.log"

Loop, Read,% FilePath
	Total_Line := A_Index

StartLine := Total_Line - 50

Loop, Read, % FilePath
	if (A_Index > StartLine)
		Lines .= A_LoopReadLine "`n"

MsgBox, % Lines
If the files are not VERY big (bigger than the available memory), it is much more efficient to read the entire file into memory and then remove the starting lines

Code: Select all

fileread, text, %Filepath%
startpos:=instr(text,"`n",,-1,50)+1
text:=substr(text, startpos)
roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Re: Get last 50 lines

24 Aug 2021, 07:48

All great, thanks! :)
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Get last 50 lines

24 Aug 2021, 13:46

braunbaer wrote:
24 Aug 2021, 03:42
That omits the first n lines instead of returning the last n lines...
Did you even try it?

Easy example:
Spoiler
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Get last 50 lines

25 Aug 2021, 01:03

Did something with FileObject years ago -> https://github.com/jNizM/AHK_Scripts/blob/master/src/file_object/FileReadLastLines.ahk
But it is slow with really big files / logs
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
braunbaer
Posts: 483
Joined: 22 Feb 2016, 10:49

Re: Get last 50 lines

25 Aug 2021, 06:00

Xtra wrote:
24 Aug 2021, 13:46

Did you even try it?
You are right, it works. I did not realize that instr with startingpos=0 performs a backward search. I thought startingpos has to be negative for that.
sofista
Posts: 654
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Get last 50 lines

25 Aug 2021, 11:55

Try

Code: Select all

Loop, parse, text, `n, `r
{
	Line := A_LoopField
}
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Get last 50 lines

25 Aug 2021, 12:36

Here is a variation of @jNizM post:

Code: Select all

FileReadLastLines(FileName, LastLines := 5, Delim := "`n")
{
    if (IsObject(file := FileOpen(FileName, "r-d", "UTF-8"))) {
        CountLines := 0
        Loop % file.Length {
            file.Seek("-" . i := A_Index)
            if (file.Read(1) = Delim)
                if (++CountLines = LastLines)
                    break
        }
        file.Seek("-" . i)
        return LTrim(file.Read(), Delim), file.Close()
    }
    return false
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Kodakku, Noesis and 381 guests