Jump to content

Getting first 10 characters of each line in a BIG txt file


ThePoro
 Share

Recommended Posts

Hello,

I have a problem with getting first 10 characters with a big txt file, about 50k lines. I only want 10 characters of each line and save it into another file. I tried to use Stringleft every line and save it into another file but it was really slow to do it. I tried to save it into a variation like $var=$var&stringleft($file,10)&@CRLF. It's faster but still really slow.

Can anybody help me with this

Thank you so much

Link to comment
Share on other sites

@ThePoro
Did you try to use the handle of the File with FileReadToArray()?

#include <FileConstants.au3>

Global $strSourceFileName =  @ScriptDir & "\SourceFile.txt", _
       $strDestFileName = @ScriptDir & "\DestFile.txt", _
       $hdlSourceFile, _
       $arrSourceFileContent, _
       $hdlDestFile, _
       $strRandomString = "", _
       $hdlTimer

$hdlTimer = TimerInit()

$hdlSourceFile = FileOpen($strSourceFileName, $FO_OVERWRITE)

For $i = 1 To 50000 Step 1

    For $j = 1 To 30 Step 1
        $strRandomString &= Chr(Random(65, 90, 1))
    Next

    FileWriteLine($hdlSourceFile, $strRandomString)
    $strRandomString = ""
Next

FileClose($hdlSourceFile)

ConsoleWrite("It tooks " & Round(TimerDiff($hdlTimer)/1000, 2) & " seconds to generate the test file." & @CRLF)

$hdlTimer = TimerInit()

$hdlSourceFile = FileOpen($strSourceFileName, $FO_READ)
$hdlDestFile = FileOpen($strDestFileName, $FO_OVERWRITE)

;~ $arrSourceFileContent = FileReadToArray($hdlSourceFile)

;~ For $i = 0 To UBound($arrSourceFileContent) - 1 Step 1
;~  FileWriteLine($hdlDestFile, StringLeft($arrSourceFileContent[$i], 10))
;~ Next

FileWrite($hdlDestFile, StringRegExpReplace(FileRead($hdlSourceFile), '(?m)^.{10}\K.*', ''))

FileClose($hdlDestFile)
FileClose($hdlSourceFile)

ConsoleWrite("It tooks " & Round(TimerDiff($hdlTimer)/1000, 2) & " seconds to read 10 characters from each line of the source file." & @CRLF)

EDIT: with the @jchd suggestion, it is even faster!

:)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

FileDelete("output.txt")
FileWrite("output.txt", StringRegExpReplace(FileRead("input.txt"), "(?m)^.{10}\K.*", ""))

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Why not forego the RegEx and just use the count parameter in the FileRead function?

FileDelete("output.txt")
$iCharacterCount = 10
FileWrite("output.txt", FileRead("input.txt", $iCharacterCount))

 

Link to comment
Share on other sites

10 hours ago, spudw2k said:

Why not forego the RegEx and just use the count parameter in the FileRead function?

Because the OP doesn't want the first 10 characters of the file, but

17 hours ago, ThePoro said:

I only want 10 characters of each line

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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

×
×
  • Create New...