Jump to content

How to get line number where a string was found?


Recommended Posts

Hey guys.

I need to obtain the line number where a given string was found, I couldn't find a function.

All I can think of is counting the number of CR & LF characters, would that work? How can I represent them to Autoit, if that's the best solution?

Thanks for the input.

Link to comment
Share on other sites

You could use FileReadToArray and search the array to find where the string is located, the array row would be equivalent to the line number.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

StringSplit on @crlf may be enough

Local $sString = "alpha" &@crlf& "beta" &@crlf& "gamma" &@crlf& "delta" &@crlf& "epsilon"
Msgbox(0,"", $sString)

Local $n = -1, $word = "gam"
$aString = StringSplit($sString, @crlf, 3)
For $i = 0 to UBound($aString)-1
   If StringInStr($aString[$i], $word) Then $n = $i
Next
Msgbox(0,"", '"' & $word & '"' & (($n = -1) ? " not found" : " found on line " & $n+1))

 

Link to comment
Share on other sites

Unless it's a file he's reading from, then he'd still need a FileRead just to convert the read text to an array with StringSplit which FileReadToArray already does for you.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Here is another method - no array, only strings.

; Local $sString = FileRead("FullPath\FileName") ; If viewing file contents for script's accuracy, word wrap may cause visual errors.
Local $sString = "alpha" & @CRLF & "beta" & @CRLF & "gamma" & @CRLF & "delta" & @CRLF & "epsilon & gamble game"
MsgBox(0, "Original String", $sString, 3)

Local $n, $word = "gam", $iCharPos = -1, $Start = 1, $sRes = ""
While $iCharPos <> 0
    $iCharPos = StringInStr($sString, $word, 1, 1, $Start) ;  $STR_CASESENSE (1) = case sensitive, occurrence = 1
    StringRegExpReplace(StringMid($sString, 1, $iCharPos), "\R", "") ; By default \R matches @CRLF, @CR or @LF only.
    $n = @extended + 1 ; @extended contains the number of replacements performed.
    $Start = $iCharPos + 1
    If ($sRes = "") Or ($iCharPos <> 0) Then $sRes &= '"' & $word & '"' & (($iCharPos = 0) ? " not found" : " found on line " & $n) & @CRLF
WEnd

MsgBox(0, "Result", $sRes)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...