Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.


In reply to Re: Perl reading in a file and getting a string in between two strings on different lines by kennethk
in thread Perl reading in a file and getting a string in between two strings on different lines by victorz22

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-24 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found