Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Perl reading in a file and getting a string in between two strings on different lines

by kennethk (Abbot)
on Apr 17, 2017 at 21:43 UTC ( #1188177=note: print w/replies, xml ) Need Help??


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:
#!/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
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.

Sidenote 1:

my $fileContent; open(my $fileHandler, $inputFile) or die "Could not open file '$inputF +ile' $!"; { local $/; $fileContent = <$fileHandler>; } close($fileHandler);
You can rewrite this as
my $fileContent = do { open(my $fileHandler, $inputFile) or die "Could not open file '$in +putFile' $!"; local $/; <$fileHandler>; };
and avoid scoping challenges on that file handle.

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.

  • 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 victorz22 (Sexton) on Apr 17, 2017 at 22:33 UTC

    Thanks your code helped out!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1188177]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2020-05-24 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (142 votes). Check out past polls.

    Notices?