Greetings Monks -
I am trying to write a program that will read in a directory of txt files, search through each file sequentially for text matches, and subsequently output the text matches to other txt files. If the script does find matches, I would like it to copy the text to a filename that would match the file from which it came.
So far, I've gotten the text search to work, and print matches to one file, not individual files. But I can't seem to get the loop to work. To do this, I've done a $File::Find::name of the directory and outputted the list of txt files into a text file, which I've then read as an array.
Here's my code - if you can give me some help, it would be much appreciated. Thanks!
#! /opt/local/bin/perl -w
# Part 1: Write all of the filenames to Index.txt to be able to inpu
+t names as an array #
open directory1, ">./index.txt" or die "Could not create file: $!\n";
use File::Find;
my @all_file_names;
find sub {
return if -d;
push @all_file_names, $File::Find::name;
}, '*my directory*';
for my $path ( @all_file_names ) {
select directory1;
print "$path\n";
}
print STDERR "contents of directory written \n";
close directory1;
##################################################
# Part 2: Open & Read Contract List
+ #
##################################################
my $filename = 'index.txt';
open my $fh, $filename
or die "Couldn't read '$filename': $!";
#chomp @ARGV = <$fh>;
print "Processing $_"
for @ARGV;
##################################################
# Part 3: Read in the contracts and search for keywords
+ #
##################################################
open Contract1a, ">./test.txt" or die "Could not create file: $!\n";
@contracts = @ARGV;
$linescount = 20; #set how many lines after the match you'd like t
+o pull;
foreach (@contracts) {
open Contract1, "<./$_ \n";
@lines = <Contract1>;
close Contract1;
my $contents = join "", @lines;
@all = undef;
shift @all;
while ($contents =~ m/Management Fees/i) {
if ($contents =~ m/(\n.*Management Fees(.*\n){$linescount})/i) {
push (@all, "$1\n");
print STDERR "$1\n";
$contents =~ s/Management Fees//i;
}
}
select Contract1a;
print join("\n",(@all));
print "\n";
}
print STDERR "...Contract1 match complete.\n\n";
close Contract1a;>
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.