Sign in to follow this  
Followers 0
BrettF

Find a line in a file

7 posts in this topic




_FileReadToArray() + Loop + StringInString()


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Say i have a file with the lines:

by parsing the file, looking for the line. (duh)

One method (pseudocode / untested / no error checking) uses FileRead,StringInStr,StringLeft,and StringReplace

$contents = FileRead($filename,filegetsize($filename))
$line = "this is a matching line"
$line_delimiter = @CRLF
$match = StringInStr($contents,$line) 
If Not $match then 
    exit 0
else
  StringReplace(StringLeft($contents,$line),$line_delimiter,"")
  exit @extended
endif
Edited by flyingboz

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Share this post


Link to post
Share on other sites

If you are going to #include <array.au3>

Then also take a look at the _Array Search() function


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

This may make it simple... the _ArrayDisplay2d is just to show you, isn't needed for the function.

Input the file to search, the string to find, and 1 in the $iCase parameter if you want the search case sensitive.

It will return a 2 dimensional array, the [0] will contain the line number found, and the [1] will display the actual line itself.

CODE
$a = _StringReturnLine('somefile.txt', 'some text')
_ArrayDisplay2d($a)

Func _StringReturnLine($hFile, $sFind, $iCase = 0)
    If Not FileExists($hFile) Then Return SetError(1, 0, 0)
    Local $aSplit = StringSplit(StringStripCR(FileRead($hFile)), @LF)
    If @error Then Return SetError(2, 0, 0)
    Local $sHoldLineNum, $sHoldLineStr
    For $iCC = 1 To UBound($aSplit) - 1
        If StringInStr($aSplit[$iCC], $sFind, $iCase) Then
            $sHoldLineNum &= $iCC & Chr(1)
            $sHoldLineStr &= $aSplit[$iCC] & Chr(1)
        EndIf
    Next
    If Not $sHoldLineNum Then Return SetError(3, 0, '')
    $sHoldLineNum = StringSplit(StringTrimRight($sHoldLineNum, 1), Chr(1))
    $sHoldLineStr = StringSplit(StringTrimRight($sHoldLineStr, 1), Chr(1))
    Local $aReturnArray[UBound($sHoldLineNum)][2]
    $aReturnArray[0][0] = UBound($sHoldLineNum) - 1
    For $iCC = 1 To UBound($sHoldLineNum) - 1
        $aReturnArray[$iCC][0] = $sHoldLineNum[$iCC]
        $aReturnArray[$iCC][1] = $sHoldLineStr[$iCC]
    Next
    Return $aReturnArray
EndFunc


Func _ArrayDisplay2D($aArray, $sTitle = 'Array Display 2Dim', $iBase = 1, $sToConsole = 0)
    If Not IsArray($aArray) Then Return SetError(1, 0, 0)
    Local $sHold = 'Dimension 1 Has:  ' & UBound($aArray, 1) -1 & ' Element(s)' & @LF & _
            'Dimension 2 Has:  ' & UBound($aArray, 2) - 1 & ' Element(s)' & @LF & @LF
    For $iCC = $iBase To UBound($aArray, 1) - 1
        For $xCC = 0 To UBound($aArray, 2) - 1
            $sHold &= '[' & $iCC & '][' & $xCC & ']  = ' & $aArray[$iCC][$xCC] & @LF
        Next
    Next
    If $sToConsole Then Return ConsoleWrite(@LF & $sHold)
    Return MsgBox(262144, $sTitle, StringTrimRight($sHold, 1))
EndFunc

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

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  
Followers 0