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

Comments

Popular posts from this blog

Automatically mount NVME volumes in AWS EC2 on Windows with Cloudformation and Powershell Userdata

Extending the AD Schema on Samba4 - Part 2

Python + inotify = Pyinotify [ how to watch folders for file activity ]