Re: How can i pull out the first line from every file in a directory



On 12/01/2006 02:01 PM, Dennis Bourn wrote:
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";
}
}
------------------------------

I finally made it to page 132 in "Learning Perl" and discovered the command line syntax.

perl -n -w -e 'if (/\d+\.\d+\.\d+\.\d+/){ print "$&\n";}' mail/mail*

that single command does most of what i need, I can just pipe the output to pfctl to add it to the firewall block list. however i still have the problem of it checking the entire file instead of the first line. In addition im seeing a version number in the headers like V6.2000.20.5 which matches my regular expresion. If i can narrow it down to the first line of the file this wont be a problem.


Use the Regexp::Common::net module for this.


.



Relevant Pages

  • Re: How can i pull out the first line from every file in a directory
    ... Each quarentined email is kept in one directory, the IP address of the sender is in the first line of the headers. ... use strict; ... the second one searches a single file for IP addresses. ... that single command does most of what i need, I can just pipe the output to pfctl to add it to the firewall block list. ...
    (perl.beginners)
  • Re: [OT] Computed regexps
    ... problem - anytime you test for a condition, ... But simple commands with a single file argument can have the same ... "file.txt" may disappear before the output of stat is printed, ... I'm struggling to find a single command or operation that is ...
    (comp.lang.awk)
  • Re: SLRN random-sig files, found this but stilll
    ... > looking to set up random sig with SLRN I found this link: ... > At the end it says one need to create for each quote a single file, ... with a dot at the beginning of the filename. ...
    (Debian-User)
  • Re: Repeated hassles moving large file...?
    ... folders, it doesn't accept single file names in the path, for single files place the filename at the end, like so: ... that would allow me to specify the destination? ... If you are using the GUI version you will have to experiment with it, I have little experience with the GUI version. ...
    (microsoft.public.windowsxp.general)
  • Re: my own perl "dos->unix"/"unix->dos"
    ... Robert Wallace wrote: ... > use strict; ... use warnings; ... > #setting based on filename argument. ...
    (comp.lang.perl.misc)