Tom Smith wrote:
> John W. Krahn wrote:
>>
>> You could use some of Perl's idioms to do what you want:
>>
>> #!C:\Perl\bin\perl.exe
>> use strict;
>> use warnings;
>>
>> @ARGV = glob 'maillog*' or die "No maillog files found.\n";
>>
>> while ( <> ) {
>>     print if /(?:imapd|pop3d).*?Log.*?user/;
>>     }
> 
> Ok,  here's the script that worked...
> 
> ----------
> #!C:\Perl\bin\perl.exe
> 
> use strict;
> use warnings;
> use Carp;
> 
> @ARGV = glob 'maillog*' or croak "Can't find any maillogs. $!";
> open(FILE,'>parsedmail.txt') or croak "Can't open parsedmail.txt. $!";
> while ( <> ) {
>    print FILE if /.*imapd.*Log.*user.*$/ || /.*pop3d.*Log.*user.*$/;
> }
> close FILE;
> 
> @ARGV = glob 'messages*' or croak "Can't find any maillogs. $!";
> open(FILE,'>parsedmessages.txt') or croak "Can't open
> parsedmessages.txt. $!";
> while ( <> ) {
>    print FILE if /.*imapd.*Log.*user.*$/ || /.*pop3d.*Log.*user.*$/;
> }
> close FILE;
> ----------
> 
> This outputs the expected data to the two files in question. I'm rackin'
> my brain a bit over something else, now. (If you haven't noticed yet,
> I'm quite new to Perl.)
> 
> I need to sort this output by day, then by time. Here's a sample of the
> first few columns of the maillog and messages files:
> 
> ---------- maillog
> Dec  5 13:51:55 server ipop3d...
> Dec  5 13:51:55 server ipop3d...
> 
> ---------- messages
> Dec 27 13:23:43 server ipop3d
> Dec 27 11:27:17 server imapd
> 
> Basic Linux log files.
> 
> Can anyone point me in the right direction for doing this? I'd like to
> understand the why as well as the how so pointers to docs would be nice,
> I'm just not getting the sort() docs at perldoc.perl.org.

If you are guaranteed to have the same month name on every line then just
using sort will work:

@ARGV = glob 'maillog*' or croak "Can't find any maillogs.";
open FILE, '>', 'parsedmail.txt' or croak "Can't open parsedmail.txt. $!";

print FILE sort grep /(?:imapd|pop3d).*Log.*user/, <>;

close FILE;


If however there will be different month names then something like this should
work:

my %mon2num = qw/Jan 01 Feb 02 Mar 03 Apr 04 May 05 Jun 06
                 Jul 07 Aug 08 Sep 09 Oct 10 Nov 11 Dec 12/;
my %num2mon = reverse %mon2num;

@ARGV = glob 'maillog*' or croak "Can't find any maillogs.";
open FILE, '>', 'parsedmail.txt' or croak "Can't open parsedmail.txt. $!";

print FILE
      map  { s/^(\S+)/$num2mon{$1}/; $_ }
      sort
      map  { s/^(\S+)/$mon2num{$1}/; $_ }
      grep /(?:imapd|pop3d).*Log.*user/,
      <>;

close FILE;




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/


Reply via email to