Jump to content

iterating through a file


Recommended Posts

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?

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

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