Help Parsing A Text File

Discussion in 'Perl Misc' started by greggiefen, Jan 3, 2007.

  1. greggiefen

    greggiefen Guest

    Hello,

    I am new to Perl but have programmed in other languages in school. I
    am trying to parse a VERY large text file. I initially tried to load
    it all into an array but that seemed to fail due to memory constraints.

    This is what i'm trying to do:#!/usr/bin/perl -w

    open(OF, "output.txt");

    while ($line = <OF>) {

    #If a line contains this i wanted it printed so the date will be
    included
    if($line =~ /Page 1/) {
    print $line;
    }

    #I want all the records regarding Mr. Smith printed. However the
    records span multiple lines
    if($line =~ /Mr Smith/) {
    print $line;

    #the lines of the peronsal records end in a line of all asterix
    while ($line =~ /[^\*]/) {
    print $line;
    #do something magical here
    }
    }

    }

    What I need to do is to somehow get the perl script to jump to the next
    line after printing the current line in the last loop, but still stay
    in the loop until it reached the line of all **. If i could get all of
    it into an array i could simply incriment the index until i reched the
    line of all asterixs.
    greggiefen, Jan 3, 2007
    #1
    1. Advertising

  2. greggiefen

    -berlin.de Guest

    greggiefen <> wrote in comp.lang.perl.misc:
    > Hello,
    >
    > I am new to Perl but have programmed in other languages in school. I
    > am trying to parse a VERY large text file. I initially tried to load
    > it all into an array but that seemed to fail due to memory constraints.
    >
    > This is what i'm trying to do:#!/usr/bin/perl -w
    >
    > open(OF, "output.txt");
    >
    > while ($line = <OF>) {
    >
    > #If a line contains this i wanted it printed so the date will be
    > included
    > if($line =~ /Page 1/) {
    > print $line;
    > }


    Is there a question involved?

    > #I want all the records regarding Mr. Smith printed. However the
    > records span multiple lines
    > if($line =~ /Mr Smith/) {
    > print $line;
    >
    > #the lines of the peronsal records end in a line of all asterix
    > while ($line =~ /[^\*]/) {
    > print $line;
    > #do something magical here
    > }
    > }
    >
    > }
    >
    > What I need to do is to somehow get the perl script to jump to the next
    > line after printing the current line in the last loop, but still stay
    > in the loop until it reached the line of all **. If i could get all of
    > it into an array i could simply incriment the index until i reched the
    > line of all asterixs.
    >


    That's what the ".." operator does in scalar context. Try

    print if /Mr Smith/ .. /^\*+$/;

    Anno
    -berlin.de, Jan 3, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. .Net Sports
    Replies:
    11
    Views:
    1,414
    .Net Sports
    Jan 17, 2006
  2. Kai Schlamp
    Replies:
    1
    Views:
    409
    Arne Vajhøj
    Mar 27, 2008
  3. William Gill

    Help parsing a text file

    William Gill, Aug 29, 2011, in forum: Python
    Replies:
    6
    Views:
    285
    William Gill
    Sep 1, 2011
  4. Joey Martin

    Help with parsing text file

    Joey Martin, Nov 7, 2003, in forum: ASP General
    Replies:
    2
    Views:
    92
    dlbjr
    Nov 7, 2003
  5. Domenico Discepola

    Assistance parsing text file using Text::CSV_XS

    Domenico Discepola, Sep 1, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    442
    Domenico Discepola
    Sep 2, 2004
Loading...

Share This Page