From: "John W. Krahn" <[EMAIL PROTECTED]>
> Tim Martin wrote:
> > 
> > My new manager wants me to learn perl and start with understanding
> > logs files from 50 plus servers. I hope I have sent this message to
> > the correct perl form. I have a sample of the logs below that need
> > to be fixed using perl. Any ideas or suggestion will be helpful.
> > 
> > =============  Start of log sample  =============
> > 
> > 1.  REMOVE ALL WHITE SPACE GREATER THEN ONE.
> > 
> > [snip]
> > ====================================================================
> > =======
> > 
> > 2.  ENSURE EACH LINE IN THE LOG START WITH A DATE AND TIME
> >      REMOVE THE CARRIAGE RETURN AND LINE FEED
> >      INSERT ONE WHITE SPACE
> > 
> > [snip]
> > ====================================================================
> > =======
> > 
> > 3.  ENSURE EACH LINE IN THE LOG START WITH A DATE AND TIME
> >      REMOVE THE CARRIAGE RETURN AND LINE FEED
> >      INSERT ONE WHITE SPACE
> >      REMOVE ALL BLANK LINES FROM LOGS
> > 
> > [snip]
> > =============  End of log sample  =============
> 
> 
> perl
> -lane'eof||m!^\d{4}/\d\d/\d\d\s+\d\d:\d\d:\d\d\s!?((@X&&print"@X"),@X=
> @F):push@X,@F' logfiles*

Wow :-)

It's very short and all, but ... not very readable :-)

Let's write it as an ordinary program:

#!perl -w
use strict;

sub CleanupLog {
        my ($from_file, $to_file) = @_;
        open my $IN, '< ' . $from_file 
                or die "Can't open file $from_file : $!\n";
        open my $OUT, '> ' . $to_file
                or die "Can't create file $to_file : $!\n";
        my $line = '';
        while (<$IN>) {
                chomp; # get rid of the newline
                next if $_ eq ''; # skip empty lines
                s/\s{2,}/ /g; # replace 2 or more whitespace chars
                        # with a single space
                if ( m{^\d{4}/\d{2}/\d{2} \d\d:\d\d:\d\d} ) {
                        # if the current line starts with a timestamp ...
                        # I assume the YYYY/MM/DD HH:MI:SS format
                        print OUT $line, "\n" if $line;
                                # print the buffer
                        $line = $_;
                                # remember the current line
                } else {
                        $line .= ' ' . $_; 
                                # add the current line to the buffer
                }
        }
        print $OUT $line, "\n" if $line;
                # print the last buffer
        close $IN;
        close $OUT;
}

# now let's process all .log files in the current directory and 
# write the results to 'Cleaned' subdirectory
mkdir 'Cleaned', 0777 unless -d 'Cleaned';
opendir my $DIR, '.';
while (defined(my $file = readdir $DIR)) {
        next unless $file =~ /\.log$/i; 
                # skipp all files that do not end with '.log'
        next if -d $file; # skip directories
        print "$file\n";
        CleanupLog $file, 'Cleaned/'.$file;
}
closedir $DIR;
__END__

Of course you'll have to read some introductory docs to be able to 
modify this to fit better to your needs, but this should give you a 
start.

Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to