in reply to Perl reading in a file and getting a string in between two strings on different lines

So the output i am getting gathers everything up to the $endString but fails to ignore everything before the $beginString.
SEARCH => #Beginning of data I need ....... ........ #all the data I need ....... ..... #End of Data I Need TEST =>
  • Comment on Re: Perl reading in a file and getting a string in between two strings on different lines
  • Download Code

Replies are listed 'Best First'.
Re^2: Perl reading in a file and getting a string in between two strings on different lines
by Anonymous Monk on Apr 17, 2017 at 21:23 UTC
    my $beginString = "SEARCH"; my $endString = "TEST"; my $fileContent; open(my $fileHandler, $inputFile) or die "no read '$inputFile' $!\n"; while (<$fileHandler>) { print if /$beginString/ .. /$endString/; } close $fileHandler;

      That didn't work the output i got was just the whole file, i even tried to put an 's' at the end of it for many lines but that didn't work either.

        Show input that doesn't work! See SSCCE

        That's a classic, most likely you are doing it wrong.

        The only possibility top make it foolproof is to quote regex metacharacters and to anchor the line's start with ^

        print if /^\Q$beginString/ .. /^\Q$endString/;

        See quotemeta

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!