Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Finding and extracting lines of text

by mikevanhoff (Acolyte)
on Jul 29, 2003 at 19:47 UTC ( [id://278956]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all, This seemed likan easy thing to do, but I am not getting it figured out. I hope someone can help. I have a text file that contains the result of a Sybase data dump. I would like to extract the final dump line which gives the total kb dumped to tape, and then a few lines down the completion results. I can find the 1st line from the a one liner (perl -ne /Dump phase number 3/ dump.log), but when I try to put it into a while loop so I can stop searching, it never finds the /Dump phase number 3/ expression. ANY suggestions to help will be appreciated. Thanks in advance. A sample of the code is below
# open(FILE,"dumptape.log") || die("Could open file"); $line = <FILE>; # while(<FILE>) { print if /Dump phase number 3/ ; } close FILE;

Replies are listed 'Best First'.
Re: Finding and extracting lines of text
by Cine (Friar) on Jul 29, 2003 at 20:35 UTC
    That seems ok, except that you are skipping the first line.
    Btw. You should print the error message when you have problems opening a file, as in die "Could not open filename: $!";

    T I M T O W T D I
      Thanks Tim, It is good to know I am heading in the right direction, but as I said it does not work. The script completes, ut it does NOT print the line. Then on to the next step, How do I skip the next 2 lines and print the 3rd line from where I "found" the test expression? Thanks again.
        If its just for you and not for processing, then perl is overdoing it and grep -A3 "line searching for" file.log is much easiere.
        Else you have to make a little state machine to handle the cases properly (as in before found, searching for lines between, printing the next info and searching for the next data)

        T I M T O W T D I
Re: Finding and extracting lines of text
by ajdelore (Pilgrim) on Jul 29, 2003 at 22:00 UTC

    This will print out the matched line, and then the three lines following it. This, if I understand correctly, is what you are trying to do.

    open(FILE,"dumptape.log") or die ("Could not open file"); while(<FILE>) { if (/Dump phase number 3/) { print; print <FILE> for (1..3); last; } } close FILE;

    </ajdelore>

      Anthony, Thanks for the code. It does almost what I need it to do. It prints the line I seek, but I need to skip the next 2 lines and print the 3rd line. It happens that if the data dump is successful, this line would then also be the last line in the log. Once I can extract the correct lines from a successful dump log, I will need to handle the case of a failed dump log which should come in an else clause of the if statement. does this make sense, or should I make a better explaination. Thanks in advance.
        so just throw away the first two lines. Here's a quick fix on ajdelore's code

        $success = 0; open(FILE,"dumptape.log") or die ("Could not open file"); while(<FILE>) { if (/Dump phase number 3/) { print; <FILE> for (1..2); print <FILE>; $success = 1; last; } } if(!$success) { print "Failed dump log\n"; #do other failed dump log stuff here } close FILE;

        It seems like you have an understanding of some areas of perl, but are lacking some of the basics. You should look for an online tutorial (or better, one of the O'Reilly books), to pick up on a few of the little things you're missing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://278956]
Approved by Thelonius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2024-04-19 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found