Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Print area between two lines within a file.

by c (Hermit)
on Sep 06, 2001 at 21:35 UTC ( [id://110652]=perlquestion: print w/replies, xml ) Need Help??

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

I am wanting to print all the lines between two lines containing the word USE. I quickly jotted down:

#!/usr/bin/perl -wT use strict; open(FH, "file"); while(<FH>) { next until (/USE/); my $line = s/\\//g; print $line until (/USE/); } close(FH);

I realized after a few trials and only printing lines containing the word USE that each line will be judged against that first next statement causing it to be passed over. I feel as though I have the right idea, but I am missing some small detail. Can someone point me towards the right path?

humbly -c

Replies are listed 'Best First'.
Re: Print area between two lines within a file.
by Hofmator (Curate) on Sep 06, 2001 at 21:43 UTC

    Will something like this do?

    while (<DATA>) { if (/use/.../use/) { print; } } __DATA__ before use first second use last

    -- Hofmator

      If (as I seem to understand from your post) you do not want to print the use lines, change the condition to

      /use/... /use/ and !/use/

      -- TMTOWTDI

Re: Print area between two lines within a file.
by andye (Curate) on Sep 06, 2001 at 21:42 UTC
    Something like this...
    while(<FH>) { print if /USE/.../USE/; }
    andy. update: thanks Hofmater and nardo, third dot inserted.
      That should be 3 dots rather than two. Because the right operand is true whenever the left is true you want to use three so that the right isn't evaluated when the left gets evaluated to true, with two dots it will only print out lines with USE in them and nothing in between.
Re: Print area between two lines within a file.
by jryan (Vicar) on Sep 06, 2001 at 21:50 UTC
    # put data into an array open(FH, "file"); my @in=<FH>; close (FH); # loop through array for (my $i=0; $i<@in; $i++) { # if the line contains use... if ($in[$i] =~ /USE/) { # skip the line that had use in it, and loop # until the array is done or USE is encountered for ($i++; $in[$i] =~ /USE/ && $i<@in; $i++) { print $in[$i]; } } }

    Not as elegent as some of my fellow monks could probably come up with, but it'll do what you want.

    Update: I must be the slowest typer on the site...
Re: Print area between two lines within a file.
by VSarkiss (Monsignor) on Sep 06, 2001 at 21:47 UTC

    Well, I'm not sure if this is very idiomatic, but here's how I've done stuff like this in the past:

    my $toggle = 0; # initially false open FH, 'file' or die "open fails: $!"; while (<FH>) { $toggle = !$toggle if /USE/; # Not sure why you'r removing backslashes, but.... # tr/\\//d would be faster, BTW s/\\//g; print if $toggle; } close FH;
    I just had to add the "or die" to your open....

    HTH

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 05:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found