ranrodrig has asked for the wisdom of the Perl Monks concerning the following question:

Folks, I need you help cuz I have a 200M ascii file (226'492,416 chars) in a single line and I need to read only the first 3 million of chars

I already tried with head, more, and a "c" pgm, with no luck.

Can you help me with this?, I'm a very novice to Perl and the info in the file is similar to this: $ĈI$ĈI$ĈI$ĈI$ĈI$ĈI$ĈI$ĈI, ◄◄◄◄◄◄◄◄◄, etc.

TIA for your help

  • Comment on Need to read certain number of chars in a file

Replies are listed 'Best First'.
Re: Need to read certain number of chars in a file
by Marshall (Canon) on Oct 18, 2010 at 15:56 UTC
    Take a look at sysread and read. If these are single byte characters, just do a 3MB read().

    Update: looks like read() can take into account UTF-8 characters if that is what you have. Otherwise if just plain ASCII, then its one byte per character.

Re: Need to read certain number of chars in a file
by halfcountplus (Hermit) on Oct 18, 2010 at 16:16 UTC

    Dunno how you can fail to do this with a C prog -- this is one of the rare instances where it would probably be simpler.

    Anyway, if you want to work with individual characters in perl here's some clues:

    #!/usr/bin/perl -w use strict; open (IN, "<$0") || die; # $0 is this script my $data; read(IN,$data,30); # read in first 30 characters close(IN); # now split those into an array of individual values my @chars = unpack('c*',$data); print chr($_)."\n" foreach (@chars);

      Guys thanks a lot for your quick answer and you were rigth about the C program it was easier and simpler

      #include <stdio.h> main() { int c, i = 1; while (i <= 100){ c = getchar(); putchar(c); i++; } }

        The same in Perl:

        print getc() for 1..100;