It's hard to recommend a how to go through the file, without seeing sample input. You should probably also think about chosing a pattern so you don't need to regenerate multiple paterns each line, such as:

my $ports = '(?:'.join('|', @win_ports).')'; while (<LOG>) { print if ( m#$regex/$ports#o ); }

I also noticed you never did anything with @trojan_ports.

Update: I realized that this also didn't deal with part #2 of your issue -- breaking things down by port. Assuming you had enough memory to deal with keeping the whole thing in memory, I'd probably push the records into arrays in memory, and then print them out when done, given the considerations. However, I still have no idea what the format of the log files is, and it's entirely possible that you may have multiple matches per line, if you have both the remote and local ip/port combination. I'd probably use logic similar to:

my $atom = qr/[1,2]?\d{1,2}/; my @ports_win = qw( 135 137 139 445 1025 1433 1434 ); my @ports_trojan = qw( 114 15118 4899 ); my $file = 'C:\fw.log'; my $ports = '('.join('|',@ports_win, @ports_trojan).')'; my $ip = qr#$atom\.$atom\.$atom\.$atom/$ports#; my %lines = (); open LOG, '<', $file or die "Can't read from $file : $!"; while ( my $line = <LOG> ) { if ( $line =~ m/$ip/o ) { push ( @{$lines{$1}}, $line ); } } foreach my $port ( 'WINDOWS', @ports_win, 'TROJAN', @ports_trojan ) { print "\n\n$port\n-------\n",@{$lines{$port}||[]}; }

In reply to Re: Firewall Log Analysis - port matrices by jhourcle
in thread Firewall Log Analysis - port matrices by monger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.