Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

doing something with the rest of a file after matching a keyword

by Anonymous Monk
on Jan 17, 2003 at 16:38 UTC ( [id://227718]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks, I am very new to perl and would like some help. I am basically reading in a file, matching a key-word, and I then want to somehow save the rest of the file from that point onwards into a variable.... i have not idea how to do the last part!
if ($word eq 'Well') { # save everything from now on }
can anyone help??

Replies are listed 'Best First'.
Re: doing something with the rest of a file after matching a keyword
by John M. Dlugosz (Monsignor) on Jan 17, 2003 at 16:54 UTC
    Arrange so the <READ> operator will grab the entire rest of the file, not just the next line. To do that, read the description of $/ (a.k.a. Input Record Separator) in the perlvar document (part of the perlxxx man page set that came with Perl).

    —John

Re: doing something with the rest of a file after matching a keyword
by Hofmator (Curate) on Jan 17, 2003 at 17:11 UTC
    Depending on how you process the file (line by line, as a whole, ...) and on how you find the keyword, the following might be another alternative.

    Slurp the whole file into a variable and use a regex to extract the part you want - or delete the part you don't want.

    open my $file, '<', "$filename" or die $!; my $text = do { local $/; <$file> }; # get the part you want my ($wanted) = $text =~ /Well(.*)/s; # or delete the stuff you don't want $text =~ s/.*Well//s;
    You might have to 'massage' the regex a little to fit your exact needs ...

    -- Hofmator

Re: doing something with the rest of a file after matching a keyword
by hardburn (Abbot) on Jan 17, 2003 at 17:14 UTC

    Undef the input seperator variable ($/) after you find it:

    my $data; while($data = <FILE>) { chomp $data; $/ = undef if($data eq 'Well'); }
      some remarks:
      • it's not nice to fiddle around with the global variable $/. This might affect other parts of the program, you should localise it before usage, in this case wrap your code in
        { local $/ = $/; # your code goes here }
      • I don't think this is working as you expect it to work. Where do you want to do something with the rest of the file? If you try to do this after the loop you will find that $data is undef (because the last value <FILE> returns is undef - which terminates the while loop). If you want to deal with $data within the loop you need some extra condition to find out whether you have the right piece of the file in $data or not.

      -- Hofmator

Re: doing something with the rest of a file after matching a keyword
by OM_Zen (Scribe) on Jan 17, 2003 at 19:50 UTC
    Hi,

    while (<fname>){ $_ =~/well/ ? {$/ = undef;$Well.=$_;}:next; }
Re: doing something with the rest of a file after matching a keyword
by krujos (Curate) on Jan 17, 2003 at 16:50 UTC
    You could just set a flag and save when that is true.
    my $flag = false; my @saved #the saved portion of the file while (<FILE>) { #set the flag to true when we find the word Well if (/Well/) { $falg = true; } if ( $falg ) { push @saved, $_; #$_is the current line in your file } }

      That's far too complex.

      my @saved; while (<FILE>) { @saved = <FILE> if /Well/; }
      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 06:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found