#!/usr/local/bin/perl # This is a script that takes in an Apache log file and splits it up # into flat files based on IP addresses in the log file, or into # directories fires and then puts logs for each ip in an individual # file. Nick Chernyy ### CONFIGURATION SECTION ### # set the output dir here $outdir = "/the/directory/you/want/the/files/to/be/placed/in"; # set to one if you want to place everything into a separate dir based # on the first octet of the ip address, zero if you want all the files # to go to the same directory $presort = 1; $logfile = shift; open(logfile) or die("can't read the file $logfile...giving up."); foreach $line () { chomp($line); $n++; @data = split(/-/, $line); $ip = shift @data; @data = split(/\./, $ip); $octet = shift @data; if ($presort == 0) { open(fn,">>$outdir/$ip") or die ("can't open file $ip for writing..giving up."); print fn "$line\n"; close(fn); } else { $ddir = "$outdir/$octet"; if (!-e $ddir) { mkdir("$outdir/$octet", 0755) or die ("can't make directory for octet. ($!)"); } open(fn,">>$outdir/$octet/$ip") or die ("can't open file $ip for writing..giving up."); print fn "$line\n"; close(fn); } } close(logfile);