Jump to content
blumi

Search line of a file multiple times

Recommended Posts

blumi

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)

 

Share this post


Link to post
Share on other sites
Jfish

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

 

  • Like 1

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

Share this post


Link to post
Share on other sites
Bilgus

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

Share this post


Link to post
Share on other sites
Jfish

@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

Share this post


Link to post
Share on other sites
benners

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

Share this post


Link to post
Share on other sites
benners

It's not mine either unfortunately. I find myself amazed and inspired by the likes of mikell and Malkey who have both helped me in the past.

Share this post


Link to post
Share on other sites
BrewManNH
2 hours ago, Bilgus said:

If the file has more than 65535 lines

An array can have more than 65K rows.

  • Thanks 2

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

Share this post


Link to post
Share on other sites
Bilgus

you are right 

16,777,216

 Maximum number of elements for an array.

 

I was trying to figure out where I got 16 bits max array elements I even checked 3.2.12 and it still stated 16 million elements

Share this post


Link to post
Share on other sites
BrewManNH

_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

Share this post


Link to post
Share on other sites
blumi

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

Share this post


Link to post
Share on other sites
benners

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

  • Thanks 1

Share this post


Link to post
Share on other sites
blumi
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

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

×

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.