I have a log file that I need to divide into 'good' and 'bad' arrays based on whether or not the line contains a website listed in another array. In the snippet below, I am just trying to get the lines that match one of the sites to go in the 'bad' array, but its returning everything. The $sites file just contains a list of websites, one per line (www.yahoo.com, etc) and the $log file contains all kinds of info per line, including a website.
open ($sites_fh, '<', $sites)or die "Can't open '$sites': $!";
chomp(@sites = <$sites_fh>);
open ($log_fh, '<', $log) or die "Can't open '$log': $!";
while (<$log_fh>) {
foreach my $site (@sites) {
push @bad_log, $_ if ($_ =~ /$site/);
}
}
print "@bad_log\n"; #currently returns entire $log
any help is appreciated
$sites sample:
www.yahoo.com
www.google.com
www.comcast.com
$log sample:
X456 TV-yes DB-no 123.12.23.45 dealio3 www.google.com-------- FX-yes d
+53 Y-03
X123 TV-yes DB-yes 34.154.43.21 dealio1 www.ask.com-------- FX-no d01
+Y-03
X412 TV-no DB-no 192.365.25.23 rayovac2 www.microsoft.com--- FX-yes d1
+3 Y-07
with the samples above, only the first line of $log should end up in @bad_log, and the others would go to @good_log, once thats created
-
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.