Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Getting the next line after a match

by dthacker (Deacon)
on Dec 20, 2001 at 02:20 UTC ( #133289=perlquestion: print w/replies, xml ) Need Help??

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

Greetings Fellow Monks! I've spent some Super Search time on this but I'm not seeing the answer. I need to pull a line of data out of the output of an Informix database utility. It looks like this:
DDR -- Running -- # Event Snoopy Snoopy Replay Replay Current Current Buffers ID Position ID Position ID Position 1024 1667721 2975e7c 1667717 6d44e10 1667721 2982000
I want the line of numbers after the the line that begins with "Buffers." A co-worker pointed out that I can just match the first line that begins with a digit. True, but that may not always be so! How could I get the next line after the line that starts with "Buffers" regardless of what it started with? TIA
Dave

Replies are listed 'Best First'.
Re: Getting the next line after a match
by danger (Priest) on Dec 20, 2001 at 02:28 UTC

    Ignore lines until you see Buffers, then read in another line:

    while(<DATA>){ next unless /^Buffers/; my $line = <DATA>; print $line; } __DATA__ 100000 # Event Snoopy Snoopy Replay Replay Current Current Buffers ID Position ID Position ID Position 1024 1667721 2975e7c 1667717 6d44e10 1667721 2982000 11111 Buffers Another good buffer line
Re: Getting the next line after a match
by rje (Deacon) on Dec 20, 2001 at 02:55 UTC
    Another way to do it is to adjust perl's input behavior slightly, in order to get it to read everything up to "Buffers" in one shot. Then, reset perl's input behavior to read line-by-line again: all that's left is the header's newline, and voila! You've got your data:

    my $header = "Buffers"; # # or do it this way: # my $header = "Buffers ID Position ID Position ID +Position"; # $/ = $header; my $junk = <DATA>; $/ = "\n"; my $header_end_of_line = <DATA>; my $data = <DATA>; print $data; __DATA__ DDR -- Running -- # Event Snoopy Snoopy Replay Replay Current Current Buffers ID Position ID Position ID Position 1024 1667721 2975e7c 1667717 6d44e10 1667721 2982000 1023 1667720 2975e7b 1667716 6d44e09 1667720 2981999
Re: Getting the next line after a match
by Juerd (Abbot) on Dec 20, 2001 at 02:43 UTC
    You can use the flip-flop operator ".." for that. Read perlop to find out how it works. Note that .. isn't a list constructing operator in scalar context.

    while (<DATA>){ next if 1 .. /^Buffers/; print; } # I copied the line with the numbers several times, # to convince myself that the script wouldn't stop # after printing just the first. __DATA__ DDR -- Running -- # Event Snoopy Snoopy Replay Replay Current Current Buffers ID Position ID Position ID Position 1024 1667721 2975e7c 1667717 6d44e10 1667721 2982000 1024 1667721 2975e7c 1667717 6d44e10 1667721 2982000 1024 1667721 2975e7c 1667717 6d44e10 1667721 2982000

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

Re: Getting the next line after a match
by melguin (Pilgrim) on Dec 20, 2001 at 02:57 UTC
    To add to the above, depending on how you would get the output:
    my $line; open(OUTPUT, "|command that gets output") or die "dead"; while(<OUTPUT>) { if (/Buffers/) { $line = <OUTPUT>; last; } } close(OUTPUT);

    This is probably right. Maybe not.

    melguin

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2022-12-05 21:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?