in reply to Out of Memory - Line of TXT too large

Depending on your needs, use either read to read a given number of bytes from a filehandle, or set $/ to the number of bytes you want to process for each call to readline resp. <>. That way, you don't need to read the whole line to operate on it.

Replies are listed 'Best First'.
Re^2: Out of Memory - Line of TXT too large
by MajinMalak (Initiate) on Jan 02, 2014 at 12:51 UTC

    Sorry I'm newish to Perl so I apologize if I'm asking simple questions.

    So all I would need to do is add $/ = 30; to my code and it should only read in the first 30 characters? So something like:

    open (LOG, $file); open (OUT, ">", $outf); $/ = 50; my $header = <LOG>; print OUT $header; while (<LOG>) { print OUT $_ if ($_ =~ /^\{\|\d{4}-\d{2}-\d{2}_\d{2}.\d{2}.\d{2}\| +LOGIN/); } close LOG; close OUT;

      Sorry - I was unclear in my first reply. You need to set $/ to a reference to the number of bytes:

      $/ = \50;

      This will make each "line" exactly 50 bytes long, except for the last "line". Also see perlvar on $/.

        First, thanks for the responses. I really appreciate the help.

        So I've been fiddling with it, and both read and readline bring in 50 bytes at a time as described (I can get that part working). However, all of the data I need will be within the first 100 characters (was using 50 before just to get the hang of it).

        Right now using read and readline I am grabbing the first 100 characters, then the next 100 characters, which either grabs text from the same line, or it can grab text from the next line. Would there be a way to grab the first 100 characters of a line, then move onto the next line ignoring everything after the first 100 characters of that line? Basically truly only looking at the first 100 characters of each line?

        For example, here are the first 100 characters of the text file I am looking at (for the line I want):

        {|2013-11-26_11.50.21|LOGIN|1384639152462.653|ATOMIC|2013-11-26_11.50.21|#|default|(nameRedacted)|B0+9D+B

        All the data I want is within the first 100 characters. I just need to grab the date and name of the person for every line containing LOGIN.

        Other lines of the text file contain non-login information, and I don't need any of that info so I need to search for lines that contain LOGIN within the first 100 characters (parse it and grab data) and ignore any line not containing it.