Archive: Search Text to Get Line of First Match


Search Text to Get Line of First Match
I am looking for a simple function to search a certain text file for a string and return the line where the first match is found. I've looked on the wiki but found no function to serve this purpose.


did you try a forum search?
perhaps this thread has some of what you're looking for :-)


if you're referring to to Stu's function, it referres to the number of lines which the string was found on. I'm looking for something much simpler: the line number of the first match.


Hmm that function doesn't return the line that it was found on though. I could probably make it do so, but you should try Instructors' TextReplace plugin first.

-Stu


Name "Output"
OutFile "Output.exe"

!include "TextFunc.nsh"
!insertmacro LineFind

!include "WordFunc.nsh"
!insertmacro WordFind

Section
StrCpy $R0 "MyString" #String for search
StrCpy $R1 "" #Result line

${LineFind} "C:\a.log" "/NUL" "1:-1" "LineFindCallback"

IfErrors 0 +2
MessageBox MB_OK "Error"
MessageBox MB_OK "$$R1={$R1}"
SectionEnd

Function LineFindCallback
${WordFind} '$R9' '$R0' 'E+1{' $1
IfErrors end

StrCpy $R1 $R8
StrCpy $0 StopLineFind

end:
Push $0
FunctionEnd