I interpret your code as searching through these contract files for the "Management Fees" string, and then printing that line and the next 19 lines. If this is true then I think you're making it too difficult by slurping the entire file into a string, and then iteratively searching through that string, each time from the beginning.
If my interpretation is correct, here is a bit of code that does the same thing but processes the files line-by-line. Also it has code, which I think you requested, creating different output files for each input file.
foreach my $contract (@contracts) { open (Contract1, "<", $contract) || die("Cannot open input file $contract: $!"); my @all; while (<Contract1>) { if (/Management Fees/i) { # Print this line push @all, $_; # And the next 19 lines too for (my $i=0; $i<($linescount-1); $i++) { my $line = <Contract1>; last if !defined $line; push @all, $line; } } } close Contract1; # Base output filename on original filename. my $outfile = "$contract".".output"; open (Contract1a, ">", $outfile) || die("Cannot open output file $outfile: $!"); print Contract1a join("\n",(@all)); close Contract1a; }
In reply to Re: Reading a Directory of Files, Searching for Text, Outputting Matches
by gmargo
in thread Reading a Directory of Files, Searching for Text, Outputting Matches
by NorthShore44
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |