Jump to content
Sign in to follow this  
erezlevi

searching string inside a file and returning line number

Recommended Posts

erezlevi

hi,

how can I search for a string after doing fileread, and returning the line number where it have been found?

Share this post


Link to post
Share on other sites
Nahuel

here's a start:

#include <File.au3>

$FilePath=
$StringToSearch=
$CaseSense=0

$Lines=_FileCountLines($FilePath)
$hFile=FileOpen($FilePath,0)

For $i=0 To Number($Lines)
    $Test=FileReadLine($hFile,$i)
    If StringInStr($Test,$StringToSearch,$CaseSense) Then
        MsgBox(0,"","The string " & $StringToSearch & " was found on line " & $i)
        ExitLoop
    EndIf
Next

FileClose($hFile)

Share this post


Link to post
Share on other sites
Nahuel

Good one. Perhaps a faster one for bigger files.

Share this post


Link to post
Share on other sites
erezlevi

Good one. Perhaps a faster one for bigger files.

hi, it isn't working for me? can you see why?

#include <File.au3>
#include <Array.au3>
Dim $myArray
$StringToSearch = "ABKNAME001"
$sFilePath="c:\2905_96xxdata.txt"
$hFile=FileOpen($sFilePath,0)
_FileReadToArray ( $hFile, $myArray)
$result = _ArraySearch ($myArray, $StringToSearch, 1)
MsgBox (0,"",$result)
If $result > -1 Then
    MsgBox(0,"","The string " & $StringToSearch & " was found on line " & $result)
EndIf
$r=_ArrayDisplay ($myArray)

Share this post


Link to post
Share on other sites
picaxe

_FileReadToArray ( $hFile, $myArray)

Try this, I think you can't use a file handle there

_#include <File.au3>
#include <Array.au3>
Dim $myArray
$StringToSearch = "ABKNAME001"
$sFilePath="c:\2905_96xxdata.txt"
_FileReadToArray ( $sFilePath, $myArray)
$result = _ArraySearch ($myArray, $StringToSearch, 1)
MsgBox (0,"",$result)
If $result > -1 Then
    MsgBox(0,"","The string " & $StringToSearch & " was found on line " & $result)
EndIf
Edited by picaxe

Share this post


Link to post
Share on other sites
GEOSoft

hi, it isn't working for me? can you see why?

#include <File.au3>
#include <Array.au3>
Dim $myArray
$StringToSearch = "ABKNAME001"
$sFilePath="c:\2905_96xxdata.txt"
$hFile=FileOpen($sFilePath,0)
_FileReadToArray ( $hFile, $myArray)
$result = _ArraySearch ($myArray, $StringToSearch, 1)
MsgBox (0,"",$result)
If $result > -1 Then
    MsgBox(0,"","The string " & $StringToSearch & " was found on line " & $result)
EndIf
$r=_ArrayDisplay ($myArray)
Possibly because you are searching for a partial string. The _ArraySearch() call as written will not allow for that.

If that's the case then try

$result = _ArraySearch ($myArray, $StringToSearch, 1, 0, 0, True)

Also see _ArraySearch() in the help file for the meanings of the other parameters.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
erezlevi

Possibly because you are searching for a partial string. The _ArraySearch() call as written will not allow for that.

If that's the case then try

$result = _ArraySearch ($myArray, $StringToSearch, 1, 0, 0, True)

Also see _ArraySearch() in the help file for the meanings of the other parameters.

thanks, it solved it! :D

Share this post


Link to post
Share on other sites
erezlevi

Try this, I think you can't use a file handle there

_#include <File.au3>
#include <Array.au3>
Dim $myArray
$StringToSearch = "ABKNAME001"
$sFilePath="c:\2905_96xxdata.txt"
_FileReadToArray ( $sFilePath, $myArray)
$result = _ArraySearch ($myArray, $StringToSearch, 1)
MsgBox (0,"",$result)
If $result > -1 Then
    MsgBox(0,"","The string " & $StringToSearch & " was found on line " & $result)
EndIf
you are right, I does not work with filehandel.

Share this post


Link to post
Share on other sites
rudi

Hi,

#include <File.au3>

$FilePath=
$StringToSearch=
$CaseSense=0

$Lines=_FileCountLines($FilePath)
$hFile=FileOpen($FilePath,0)

For $i=0 To Number($Lines)
<snip>
Why do you suggest to use "Number($Lines)" in stead of just "$Lines"? If I got the docs correctly, =_FileCountLines() always returns a numeric value anyways, what do I miss?

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites
weaponx

Hi,

Why do you suggest to use "Number($Lines)" in stead of just "$Lines"? If I got the docs correctly, =_FileCountLines() always returns a numeric value anyways, what do I miss?

Regards, Rudi.

I'm not sure why that is there. You don't need it. Maybe Nahuel has been using ActionScript 3.0 too much.

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.