I've got a 680+ MB fixed width text file. Normally no big deal, but this file is one long line. I want to put a newline at the end of every record so I can deal with this monster in a reasonable manner. Each record is 164 characters long; I wrote the following script to put in the newlines, but after running for several hours it just gave me a "Out of memory!" error and provided no other output.
open(INPUT,"filename.txt") or die "Will not open input: $!";
open(OUTPUT,">output.txt"); or die "Will not open output: $!";
my $input = <INPUT>;
my $line;
my $i;
my $length = length($input);
for ($i=0; $i<$length; $i=$i+164){
$line = substr($input,$i,164);
print "$line\n"; #Output to screen to make sure it works
print OUTPUT "$line\n";
}
Is there another way to go about this, or do I need a machine with more available RAM? I ran this on a newish Core 2 Duo with 3+ GB of ram.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.