Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Splitting up XChat log files

by Paladin (Vicar)
on Jan 12, 2003 at 06:54 UTC ( [id://226220]=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info Alan Cameron
Description: Script to split up XChat log files based on the time and date of the conversation.
#!/usr/bin/perl -w

# Script to split up XChat log files by date by: Alan Cameron
#
# Purpose: XChat stores it's log files for /msg and DCC chat's as
#          nick.xchatlog in it's log file dir.  Being the kind of
#          person I am, I like to keep log files of my conversations
#          but some people use multiple nicks so the conversations
#          end up in different files.  I wanted to have all the
#          conversations in one file, so I wrote this script to
#          split the log files up by the date of the conversation.
#          This creates a bunch of files of the form:
#          foo1042353175.log foo1042354456.log foo1042355691.log
#          one for each conversation in the original log file
#          where foo is the prefix you give it, and the number is
#          the epoch time of when the conversation took place.
#          These can be combined to form one log file with all
#          the conversations in chronological order.

use strict;
use Time::Local;
sub debug ($);

die "usage: $0 <prefix> <logfile(s)>\n" unless @ARGV;

# Set DEBUG to 1 to see progress
my $DEBUG = 0;

# Prefix for time stamped logfiles
my $prefix = shift;

# Files to be processed
my @files = @ARGV;

# Convert month names to numbers
my %months;
@months{qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/} = (0 .. 1
+1);

# For each file, read through the file, and when you find a new
# section, close the current file, and open a new one with the name
# prefix<epoch time>.log  where <epoch time> is the timestamp for
# when that conversation started.
#
# If the line doesn't start a new section, just print it to the
# current sections file.
#
# NOTE: Assumes XChat Log files always have a BEGIN LOGGING line
# as the very first line of the file.  If this behavoir of XChat chang
+es
# The script will have to be changed.
for my $file (@files) {
    open FILE, "$file" or die "Can't open $file: $!\n";
    debug "Opened $file";
    while (<FILE>) {
        if (/^\*\*\*\* BEGIN LOGGING AT (.*)/) {
            close OUT;
            my ($mon, $day, $hr, $min, $sec, $yr) =
              $1 =~ /\w+\s+(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)/;
            $mon = $months{$mon};
            my $time = sprintf "%010d",
              timelocal($sec, $min, $hr, $day, $mon, $yr);
            open OUT, ">$prefix$time.log"
              or die "Can't open $prefix$time.log: $!\n";
            debug "Opened $prefix$time.log";
        }
        print OUT $_;
    }
}

sub debug ($) {
    print STDERR $_[0], "\n" if $DEBUG;
}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://226220]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-23 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found