Sunday, July 26, 2009

Basic Perl file parser - useful for working with logs

Really I just need to remember how to do this:


#!/usr/bin/env perl
#yum log file
use warnings;
use strict;

open FILE, "/var/log/yum.log" or die $!;
while (<FILE>) {
/^(.+?)\s(.+?)\s(.+?)\s(.+?)\s(.+?)\s/;
my ($month,$day,$time,$action,$package) = ($1,$2,$3,$4,$5);
if( $action =~ /Installed/ ) {
my $suffix = 'th';
if( $day == 1 ){ $suffix = 'st'; }
if( $day == 21 ){ $suffix = 'st'; }
if( $day == 31 ){ $suffix = 'st'; }
if( $day == 2 ){ $suffix = 'nd'; }
if( $day == 22 ){ $suffix = 'nd'; }
if( $day == 3 ){ $suffix = 'rd'; }
if( $day == 23 ){ $suffix = 'rd'; }

print "$day$suffix of $month at $time\n"
}
}
close FILE
Post a Comment