Sign in to follow this  
Followers 0
yair

iterating through a file

4 posts in this topic

hi,

i have a (million lines) long text file containing rows of 6 elements (numbers)

i need to add a '0' in-between the 3rd and 4rd elements in each line.

before: 0.55 0.264 0.568 88 68 75

after : 0.55 0.264 0.568 0 88 68 75

currently this code takes 72 sec to run/save through a million lines... (intel i7/920)

#include <Array.au3>

$fileIn = FileOpen("demo.txt", 0)
$fileOut = FileOpen("output_1M.txt",2)

While 1
    $line = FileReadLine($fileIn)
        If @error = -1 Then ExitLoop
    $avArray = StringSplit($line, " ")
     _ArrayInsert($avArray, 4, "0")
    FileWriteLine($fileOut,  _arraytostring($avArray, " ",1 ))
WEnd

FileClose($fileIn)
FileClose($fileOut)

also, how do/can i overwrite the line instead of dumping to new file?

Share this post


Link to post
Share on other sites



Time this and see how long it takes:

Global $sInput = @ScriptDir & "\demo.txt", $sString
Global $sOutput = @ScriptDir & "\output_1M.txt", $hOutput, $sModString

$sString = FileRead($sInput)
$sModString = StringRegExpReplace($sString, "(\S+)\s(\S+)\s(\S+)\s(.+)", "$1 $2 $3 0 $4")
$hOutput = FileOpen($sOutput, 2)
FileWrite($hOutput, $sModString)
FileClose($hOutput)

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

PsaltyDS, your code took ~8 sec.

!yay!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

im running into "Error Allocating Memory" with 450mb> files.

i think i'll try a diffrent file reader

based on to split a 1gig file to 250mb chunks

Edited by yair

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