in reply to Re^2: Matching range of text with another string in between
in thread Matching range of text with another string in between

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;

Replies are listed 'Best First'.
Re^4: Matching range of text with another string in between
by Anonymous Monk on Apr 22, 2016 at 18:11 UTC

    $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?

      The first 14 lines are from the same source file as the rest of the script. To clarify, for the source/input file I need the first 14 lines, then anything that stars with LX* and ends with CAS* only if 00003 is in between those values, then I need the last three lines of the file.

      Good point on the "$lines" variable. I'll have to re-write that portion. I'll you all know once I've done more testing. EDIT: I think that the script can't find the file, or can't open it, but is not throwing an error.

      I apologize for the haphazard order of replies. I am getting used to this forum's format.

        Note that you read the first 14 lines from <> and the middle data from $fhi, and the last 3 lines from F ?