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

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

by victorz22 (Sexton)
on Apr 17, 2017 at 20:52 UTC ( [id://1188164]=perlquestion: print w/replies, xml ) Need Help??

victorz22 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to read in a file and gather everything in between two hash keys. I want to access everything between the $beginString and $endString variables. I have tried multiple regular expressions but haven't been able to get one to work.
my $beginString = "SEARCH"; my $endString = "TEST"; my $fileContent; open(my $fileHandler, $inputFile) or die "Could not open file '$inputF +ile' $!"; { local $/; $fileContent = <$fileHandler>; } close($fileHandler); if($fileContent =~ /\b$beginString\b(.*?)\b$endString\b/s){ my $result = $1; print $result; } print Dumper($fileContent);
  • Comment on Perl reading in a file and getting a string in between two strings on different lines
  • Download Code

Replies are listed 'Best First'.
Re: Perl reading in a file and getting a string in between two strings on different lines
by BillKSmith (Monsignor) on Apr 18, 2017 at 03:02 UTC
    Your code is shown to work with this example. Please post an example that does not!
    use strict; use warnings; use Data::Dumper; my $inputFile = \do{ "Omit this line \n" . "Foo SEARCH Part 1 \n" . "Part 2 \n" . "Part 3 TEST bar \n" . "Omit this line\n" }; my $beginString = "SEARCH"; my $endString = "TEST"; my $fileContent; open(my $fileHandler, '<', $inputFile) or die "Could not open file $! +"; { local $/; $fileContent = <$fileHandler>; } close($fileHandler); if($fileContent =~ /\b$beginString\b(.*?)\b$endString\b/s){ my $result = $1; print $result, "\n\n"; } print Dumper($fileContent); OUTPUT: Part 1 Part 2 Part 3 $VAR1 = 'Omit this line Foo SEARCH Part 1 Part 2 Part 3 TEST bar Omit this line ';

    Now do you understand "post an example"?

    Bill
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
    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.

      Thanks your code helped out!

Re: Perl reading in a file and getting a string in between two strings on different lines
by Corion (Patriarch) on Apr 17, 2017 at 20:55 UTC

    Can you show us a short example of input that fails to work but should work in your opinion?

      im sorry but im not really sure what you are asking for.
        A short example of input that fails to work but should work in your opinion.
Re: Perl reading in a file and getting a string in between two strings on different lines
by Anonymous Monk on Apr 18, 2017 at 00:55 UTC
    What output do you expect to see from this input?
    'Twas brillig, and the slithy toves SEARCH Did gyre and gimble in the wabe SEARCH All mimsy were the borogoves TEST And the mome raths outgrabe
    Reasonable that you might want to see just the borogoves, but the solutions people have given will print the wabe as well.
Re: Perl reading in a file and getting a string in between two strings on different lines
by victorz22 (Sexton) on Apr 17, 2017 at 21:19 UTC
    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 =>
      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.

Re: 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:46 UTC

    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; }
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1188164]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found