Jump to content

Search through a file, find lines with specific


Recommended Posts

What I need to do is search through a HUGE text file and find any line that has "<TYPE>:" in it. I want to add this line to an array, but only if it hasn't been already added. I can't figure out how I can manage an array like that, or search through it.

Tips/Advice/Example?

Link to comment
Share on other sites

Starting code:

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
Dim $array
; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If StringInStr($line, "<TYPE>:") Then
        $found = 0
        For $x = 0 To UBound($array) - 1
            If $line == $array[$x] Then
                $found = 1
                ExitLoop
            EndIf
        Next
        If Not $found Then
            If IsArray($array) Then
                ReDim $array[UBound($array) + 1]
            Else
                Dim $array[1]
            EndIf
            $array[UBound($array) - 1] = $line
        EndIf
    EndIf
WEnd
For $x = 0 To UBound($array) - 1
    MsgBox(0, "", $array[$x])
Next
FileClose($file)

Date from test.txt

this is a test
this is <TYPE>: another test
and another
and another one
this is <TYPE>: another test

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...