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

Thanks For the help but the ou put was the same. The script failed to ignore everything before the $beginString and it succeeded in removing everything after the $endString. Both of these strings are indented so i am wondering if the blank space is getting in the way of matching the string. Here is what the output looks like and my new code after the recommended modifications.

The failed output looks like this ........... #data i want ignored, that the program fails to remove ........... ........... SEARCH => #Beginning of data I need .......... .......... .......... #End of data I need, The program successfully removes +the data below this line TEST =>
my $beginString = "SEARCH"; my $endString = "TEST"; my $fileContent = do { open(my $fileHandle, $inputFile) or die "Could not open file '$inp +utFile' $!"; local $/; <$fileHandle>; }; if($fileContent =~ /\b\Q$beginString\E\b(.*?)\b\Q$endString\E\b/s){ my $result = $1; print $result; }
  • Comment on Re: Perl reading in a file and getting a string in between two strings on different lines
  • Select or 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 NetWallah (Canon) on Apr 17, 2017 at 23:06 UTC
    Both kennethk's code and your code work correctly for me, truncating the top and bottom parts, with indentation.

    Perhaps the code you are editing may not be the same as the one you are running ???....

            ...Disinformation is not as good as datinformation.               Don't document the program; program the document.