Jump to content

Search line of a file multiple times


 Share

Recommended Posts

I want to read a file with text line for line.

The line should be searched for a string (A70) until the end of the line is reached.

If the string is found, take the Position of the start adding 19 Digits for the end, grab the string, write into an Array and go on.

How to handle the Loop until the end of the line is reached?

 

$hFileOpen = FileOpen($TextDatei)


For $i = 1 to _FileCountLines($TextDatei)

    $zeile = FileReadLine($hFileOpen, $i)

    ;$zeile = StringRegExpReplace($zeile, "[abcdefghijklmnopqrstuvwxyz]", "")
    ;$zeile = StringRegExpReplace($zeile, "[BCDEFGHIJKLMNOPQRSTUVWXYZ]", "")
    ;$zeile = StringRegExpReplace($zeile, "[öäüÖÜÄ?ß.:,()<>*]", "")

    MsgBox(64, $ScriptName, $zeile)

    While $zeile <> -1

        If (StringInStr($zeile, "A70")) Then

            $start = StringInStr($zeile, "A7070")
            $ende = $start + 19

            MsgBox(64, $ScriptName, "Zeile: " & $i & " Start: " & $start & " Ende: " & $ende)

        EndIf

        If @error = -1 Then

            MsgBox(64, $ScriptName, "Zeilenende erreicht: ")
            ExitLoop

        EndIf

    Wend

Next

; Close the handle returned by FileOpen.
FileClose($hFileOpen)

 

Link to comment
Share on other sites

If I understand the question correctly you may consider a different approach that uses filereadtoarray.  Then you can search the array for your term with arrayfindall.  Then you can use the results to modify the original array or create a new one as shown in the below example.  That way you don't need to loop every line.

#include<Array.au3>
$testFile=@DesktopDir&"\lines.txt" ; your file here
$fileArray=FileReadToArray($testFile)
if @error <> 0 Then
    MsgBox(0,'',"Error.  This is the error code: "&@error)
EndIf
_ArrayDisplay($fileArray); show the array of lines from the opened file
$searchString="3" ; change this to what you are searching for
$results=_ArrayFindAll($fileArray,$searchString,0,0,0,1)
_ArrayDisplay($results); show the array containing all the indices for the searched term

for $a=0 to UBound($results)-1
    $addString="ABC" ; this is what I am adding to certain lines that contain the search term
    $fileArray[$results[$a]]&=$addString 
Next
_ArrayDisplay($fileArray); show the modified array with the added text

 

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

I like the approach it should be faster the only thing OP needs to be aware of is the limit on entries in an Array.. If the file has more than 65535 16,777,216 lines then he needs to do it in chunks For that matter same goes if he has more results than that as well

Edited by Bilgus
Thanks BrewManNH
Link to comment
Share on other sites

@Bilgus - I am trying to "like" your post but having tech issues with that feature :blink:.  At any rate, "like".  I think the array size is an issue in either approach - agree chunking would help if it was too large.  SQLite would also be another option. edit per @BrewManNH's correction below.

 

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

Maybe use fileread to read the file to a string. Then StringRegExp to get all the global matches.

I don't know how the file is laid out or if it's 19 digits from the A or 17 from the A70 but here is my puny attempt, assuming it's A70 then 17 digits.

#include <Array.au3>

Local $s_FileRead = FileRead(@ScriptDir & '\test.txt')
Local $as_Matches = StringRegExp($s_FileRead, '(?m)(A70\d{17,17})', $STR_REGEXPARRAYGLOBALMATCH)
_ArrayDisplay($as_Matches)

 

test.txt

Edited by benners
Link to comment
Share on other sites

2 hours ago, Bilgus said:

If the file has more than 65535 lines

An array can have more than 65K rows.

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

_ArrayDisplay can only display a max of 65K lines, maybe that's what you were thinking about. :)

 

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

Thanks for the replies.

The numbers I am looking for in the file are different, that's the reason I tried it with read line find the start Position of the beginning of the number and add some Digits to the end of the number and then snipp the hole number.

One line of the file could contain more than one number.

Only the first 3 Digits are always the same, so I want to use them as searchstring. (A70)

All numbers have the same length

some examples

A707000/2013/104800

A707020/2012/106100

A700000/2015/105000

Edited by blumi
Link to comment
Share on other sites

You're getting mixed up between characters and digits. A digit is 0-9 when its forming a number.  A and / aren't digits. Try this.

#include <Array.au3>

Local $s_FileRead = FileRead(@ScriptDir & '\test.txt')
Local $as_Matches = StringRegExp($s_FileRead, '(?m)(A70\d+/\d+/\d{1,6})', $STR_REGEXPARRAYGLOBALMATCH)
_ArrayDisplay($as_Matches)

 

test.txt

Link to comment
Share on other sites

4 hours ago, benners said:

You're getting mixed up between characters and digits. A digit is 0-9 when its forming a number.  A and / aren't digits. Try this.

#include <Array.au3>

Local $s_FileRead = FileRead(@ScriptDir & '\test.txt')
Local $as_Matches = StringRegExp($s_FileRead, '(?m)(A70\d+/\d+/\d{1,6})', $STR_REGEXPARRAYGLOBALMATCH)
_ArrayDisplay($as_Matches)

 

test.txt

Works fine now, thank you very much

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...