Dennis Bourn wrote:
> Im working on a perl script to add the IP addresses from spam to my
> blocked list. Each quarentined email is kept in one directory, the IP
> address of the sender is in the first line of the headers. Im using
> O'Reilly's "Learning Perl" as a guide and got some parts working. I can
> list all the files in the directory eaisy enough,.. i was going to have
> that script supply the filename and fire off another script which
> searched for the IP in individual files,..  I think its a waste of
> processing power to scan the entire file when i just need the first
> line. Is there an eaisy way to limit the search to the first line?
> 
> some code
> the first one returns each filename in the directory.
> ----------------------------
> #!/usr/bin/perl -w
> use strict;
> my $file;
> my $dir = "mail/";
> opendir(BIN, $dir) or die "Can't open $dir: $!";
> while( defined ($file = readdir BIN) ) {
> next if $file =~ /^\.\.?$/;     # skip . and ..
>    print "$file\n" if -T "$dir/$file";
> }
> closedir(BIN);
> -----------------------------
> 
> the second one searches a single file for IP addresses. (I left in my
> failed attempts to get the regex correct,.. figured it might make
> someone happy to know that there are worse coders out there than
> themselves)
> ------------------------------
> #!/usr/bin/perl -w
> use strict;
> while (<>){
>  if (/\d+\.\d+\.\d+\.\d+/){
>  #if (/\(*\)/){    #search for anything in brackets
>  #if (/\d\.\d\.\d\.\d/){  # search for digit.digit.digit.digit
>  #if (/([1-255]\.[0-255]\.[0-255]\.[0-255])/) { #another atempt
>    print "$&\n";
>               }
> }
> ------------------------------


#!/usr/bin/perl
use warnings;
use strict;

my $dir = 'mail';

opendir BIN, $dir or die "Can't open $dir: $!";

while ( defined( my $file = readdir BIN ) ) {

    next unless -f "$dir/$file" and -T "$dir/$file";

    open FILE, '<', "$dir/$file" or die "Can't open $dir/$file: $!";

    # only read first line of file
    my ( $ip ) = <FILE> =~ /(\d+\.\d+\.\d+\.\d+)/;

    close FILE;

    do_something_with( $ip );
    }

closedir BIN;





John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to