in reply to Matching range of text with another string in between

Welcome to the Monastery, periodicalcoder!

If the last line of each entry will always be CAS*, you can split up the file into chunks by setting the record separator ($/) to that, then configure your regex to match across newlines (/m) and to match newlines (/s). The zero-width lookahead (?:...) ensures that '00003' comes after "LX*" and before the record separator ("CAS*").

use warnings; use strict; open my $fh, '<', 'file.txt' or die $!; { local $/ = 'CAS*'; while (<$fh>){ print $_ if /LX\*.*(?=00003$)/ms; } }

Replies are listed 'Best First'.
Re^2: Matching range of text with another string in between
by GrandFather (Saint) on Apr 22, 2016 at 03:30 UTC

    The /m isn't required. /m enables the multi-line mode which allows ^ and $ to match embedded newline characters.

    Update: Argh! I missed seeing the $ in stevieb's reply. I apologise to periodicalcoder for causing confusion and to stevieb for being silly!

    Premature optimization is the root of all job security

      Stevieb and GrandFather, thank you for your replies. I feel that we are heading in the right direction, but I have a few questions.

      First, I should have been more thorough in my question. What you gave me does apply, but I also need the first 14 lines and the last 3 lines of the file. I wanted to make sure that you had this information.

      Steveieb, I used your code but I get no results. I used your code as is, and the perl command line window got no output. If I define the output file the file gets created with no content. On a guess I changed the input file name to a nonexistent file and I got the same results, and no errors. Please pardon my noobness, and I hope that you can help :)

      UPDATED: Here is what I have at the moment as I am trying to output the results to another file (the .pl file sits in the same directory as the input/output files):

      use warnings; use strict; #Note that this script throws errors when pull file paths are defined. #Must be run from the path that the input/output files exist. open my $fhi, '<', 'cr835.txt' or die "$!"; open my $fho, '>', 'cr_output.txt' or die "$!"; #Prints the first 14 lines to the output file while(<>) { 1 .. 14 ? print : last; } #Prints content starting with LX* and ending with CAS* #but only if 00003 exists { local $/ = 'CAS*'; while (<$fhi>){ print $fho if /LX\*.*(?=00003$)/s; } } #Prints the last 3 lines of the file while (<F>) { $. < $lines - 3 and print while <F> } close $fhi; close $fho;

        $lines is not defined. The /m is required for the $ in 00003$. What file do you think it is reading the first 14 lines from?