Page 1 of 1

searching for text in a fie

Posted: 26 Apr 2018, 17:36
by kallera
Hi everybody,

I have stumbled upon an issue where i desperately need some help. I need to find whether a string (for example the string "belzebub") can be found in a fairly big (atm about 20Meg file abc.log) on my disk. I know that the text is, if it is, among the last f.ex. 1000 bytes. No other occurancies are relevant but the last occurance.

So somehing like this:

found := 0
open file C:\path\abc.log/ re-read file the file abc ; re-read the file because the text, if it exists has been added fairly recently by another program
read from the end of the file
read last 1000 bytes
search for the text ”belzebub” in the last bytes searching from the end
if the text is found
{
found_pos := position ; mark position where was found
found := 1 ; mark that text was found
return
}
return ; nothing was found, all is done

Could somebody point me in the right direction? Any help would be appreciated. Which commands are necessary when opening / re-reading the file, how to tell the program where it should be read, how to search for the text, all this in to say the least unclear to me.

Thank you.

--=kallera=--

Re: searching for text in a fie

Posted: 27 Apr 2018, 02:06
by Rindis
This would be where you start looking for a solution
https://autohotkey.com/board/topic/1481 ... ion-match/



use fileread to read to the file in to a variable

Code: Select all

FileRead, Contents, C:\path\abc.log ; reads the file into a variable

Re: searching for text in a fie

Posted: 27 Apr 2018, 08:34
by Odlanir
Tested wit a 22MB file. Less than 0.8 seconds execution time.

Code: Select all

FileRead, all, abc.log
arr := strsplit(all, "`n")
loop, % arr.MaxIndex() {
    if (RegExMatch(arr[a_index], "belzebub")) {
        MsgBox % "Found at line : " a_index "`nRow contents`n" arr[a_index]
        break
    }
}
or even faster

Code: Select all

FileRead, all, abc.log
loop, parse, all, `n, `r
{   
    if (RegExMatch(A_LoopField, "belzebub")) {
        MsgBox % "Found at line : " a_index "`nRow contents`n" A_LoopField
        break
    }
}
even more fast

Code: Select all

FileRead, all, abc.log
loop, parse, all, `n, `r
{    
    IfInString, A_LoopField, belzebub
    {
        MsgBox % "Found at line : " a_index "`nRow contents`n" A_LoopField
        break
    }
}

Re: searching for text in a fie

Posted: 27 Apr 2018, 22:30
by kallera
Thank you both Rindis and Odlanir very much for your kind help and for the trouble you had answering my request for help! It was highly appreciated. I will test the solutions you suggested as soon as possible and return with more questions as needed.

Thanks!

--=kallera=--