in reply to Perl reading in a file and getting a string in between two strings on different lines
This appears to meet your spec:
Given that it's largely taken from your posted code, I wonder if you accidentally fixed your issue in preparing the post. If this does not output what you need, please describe how.#!/usr/bin/perl use strict; use warnings; use 5.10.0; use Data::Dumper; my $beginString = "SEARCH"; my $endString = "TEST"; my $fileContent; { local $/; $fileContent = <DATA>; } if($fileContent =~ /\b$beginString\b(.*?)\b$endString\b/s){ my $result = $1; print $result; } #print Dumper($fileContent); __DATA__ #Ignoring this SEARCH => #Beginning of data I need ....... ........ #all the data I need ....... ..... #End of Data I Need TEST => # And ignoring this
Sidenote 1:
You can rewrite this asmy $fileContent; open(my $fileHandler, $inputFile) or die "Could not open file '$inputF +ile' $!"; { local $/; $fileContent = <$fileHandler>; } close($fileHandler);
and avoid scoping challenges on that file handle.my $fileContent = do { open(my $fileHandler, $inputFile) or die "Could not open file '$in +putFile' $!"; local $/; <$fileHandler>; };
Sidenote 2: When using start/finish tags, assuming you want literals, you should quotemeta:
if($fileContent =~ /\b\Q$beginString\E\b(.*?)\b\Q$endString\E\b/s){
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Perl reading in a file and getting a string in between two strings on different lines
by victorz22 (Sexton) on Apr 17, 2017 at 22:33 UTC |
In Section
Seekers of Perl Wisdom