There are a few shortcuts that could make this smaller, but the code below is one way to comment lines that matched a list of words.
However, you will probably need to dial in the regex to better meet your needs.
#!/usr/bin/perl
use strict;
use warnings;
my @file_a_array;
my $newfile;
my $file_a = <path to word file>;
my $file_b = <path to lines of text>;
open (FILE_A, "$file_a") or die "couldn't open the file!";
while (my $line = <FILE_A>) {
chomp ($line);
push (@file_a_array, $line);
}
close (FILE_A);
open (FILE_B, "$file_b") or die "couldn't open the file!";
while (my $line = <FILE_B>) {
chomp ($line);
foreach my $word (@file_a_array) {
if ($line =~ /$word/) {
$newfile = $newfile . "# ";
}
}
$newfile = $newfile . $line . "\n";
}
close (FILE_B);
open (FILE_B, ">$file_b") or die "couldn't open the file!";
print FILE_B $newfile;
close (FILE_B);
print "end\n";
Hope this helps!
Jamie
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|