If you want to match across multiple lines, you need to keep multiple lines in memory, but the problem is your while (<INFILE>) only reads one line at a time. So for Athanasius's solution to work, you need to read the entire file into memory ("slurp"), which you can do like this:
open my $ifh, '<', $infile or die "$infile: $!"; my $data = do { local $/; <$ifh> }; close $ifh;
Note that you really should Use strict and warnings, and some other more modern features like lexical filehandles and the three-argument open like I am showing here.
Another approach is to read the file line-by-line, and keep some state in memory. Since you are reading from multiple input files, I suspect you might want to treat them as a continuous stream of input? If so, then for the above approach, you'd have to slurp all the files into memory and concatenate them, which might be a lot, depending on the size of the input files. But that's not necessary for the line-by-line approach:
use warnings; use strict; use File::Glob ':bsd_glob'; my @infiles = sort glob 'SBNUM_*.txt'; my $outfile = 'v2test.txt'; open my $ofh, '>', $outfile or die "$outfile: $!"; my ($prevleft,$prevright); for my $infile (@infiles) { open my $ifh, '<', $infile or die "$infile: $!"; while (<$ifh>) { my ($left,$right) = /^(\d{3}),-->(\d{3}),$/ or die "Couldn't match '$_'"; if (defined $prevleft) { print $ofh "$prevleft,-->$right,\n"; } ($prevleft,$prevright) = ($left,$right); } close $ifh; } close $ofh;
Or, just for fun, the same code as a oneliner:
perl -nle '/^(\d{3}),-->(\d{3}),$/||die;defined$x&&print"$x,-->$2,";$x +=$1' SBNUM_*.txt >v2test.txt
In reply to Re: Multi Line Regex Matches...
by haukex
in thread Multi Line Regex Matches...
by tj999
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |