#!/usr/bin/perl # log2db.pl - extract date, name, and text from irc log # Eric Lease Morgan # February 12, 2011 - first cut # configure use constant LOG => './irclog.txt'; # require use strict; # initialize my $date = ''; open INPUT, ' < ' . LOG or die "Can't open " . LOG . ": $!\n"; while ( ) { # re-initialize my $time = ''; my $text = ''; my $name = ''; # clean chop; # check for day change # --- Log opened Mon Feb 07 06:15:03 2011 if ( /^--- Log opened / ) { /^--- Log opened (.*)$/; my ( $weekday, $month, $day, $time, $year ) = split / /, $1; # map the month if ( $month eq 'Jan' ) { $month = '01' } elsif ( $month eq 'Feb' ) { $month = '02' } elsif ( $month eq 'Mar' ) { $month = '03' } elsif ( $month eq 'Apr' ) { $month = '04' } elsif ( $month eq 'May' ) { $month = '05' } elsif ( $month eq 'Jun' ) { $month = '06' } elsif ( $month eq 'Jul' ) { $month = '06' } elsif ( $month eq 'Aug' ) { $month = '08' } elsif ( $month eq 'Sep' ) { $month = '09' } elsif ( $month eq 'Oct' ) { $month = '10' } elsif ( $month eq 'Nov' ) { $month = '11' } elsif ( $month eq 'Dec' ) { $month = '12' } else { # error print "Unknown value for month: $month. Call Eric.\n"; next; } $date = $year . '-' . $month . '-' . $day; next; } elsif ( /^--- Day changed / ) { /^--- Day changed (.*)$/; my ( $dow, $month, $day, $year ) = split / /, $1; # map the month if ( $month eq 'Jan' ) { $month = '01' } elsif ( $month eq 'Feb' ) { $month = '02' } elsif ( $month eq 'Mar' ) { $month = '03' } elsif ( $month eq 'Apr' ) { $month = '04' } elsif ( $month eq 'May' ) { $month = '05' } elsif ( $month eq 'Jun' ) { $month = '06' } elsif ( $month eq 'Jul' ) { $month = '06' } elsif ( $month eq 'Aug' ) { $month = '08' } elsif ( $month eq 'Sep' ) { $month = '09' } elsif ( $month eq 'Oct' ) { $month = '10' } elsif ( $month eq 'Nov' ) { $month = '11' } elsif ( $month eq 'Dec' ) { $month = '12' } else { # error print "Unknown value for month: $month. Call Eric.\n"; next; } $date = $year . '-' . $month . '-' . $day; next; } # actual transaction else { # extract date and text /^\[(\d\d:\d\d:\d\d)\](.*)/; $time = $1; $text = $2; # skip "processing instructions", for now if ( $text =~ / ---\|/ ) { next } elsif ( $text =~ / -!-\|/ ) { next } # trap "me" comment elsif ( $text =~ / \* / ) { $text =~ s/ \*// } # clean $text =~ s/ +/ /g; $text =~ s/^ //; $text =~ s/ $//; # extract name from normal action if ( $text =~ /^\w+ \|/ ) { $text =~ /^(\w+) \| (.*)/; $name = $1; $text = $2; } # extract name for "me" action else { $text =~ /^(\w+) /; $name = $1; } } # date stamp; my $datestamp = "$date $time"; # echo #print " date stamp: $datestamp\n"; #print " name: $name\n"; #print " text: $text\n"; #print "\n"; # echo to "database" print "$datestamp\t$name\t$text\n"; } # done close INPUT; exit;