Created
January 22, 2016 11:40
-
-
Save Cougar/1d7020a89a2b9985e803 to your computer and use it in GitHub Desktop.
Split syslog file based on month field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
my $m_old = ""; | |
my $y = 0; | |
my $fd; | |
my $fn = ""; | |
my %mon = ( | |
Jan => '01', | |
Feb => '02', | |
Mar => '03', | |
Apr => '04', | |
May => '05', | |
Jun => '06', | |
Jul => '07', | |
Aug => '08', | |
Sep => '09', | |
Oct => '10', | |
Nov => '11', | |
Dec => '12', | |
); | |
while (<>) { | |
next unless (/^(...) /); | |
my $m = $1; | |
if ($m ne $m_old) { | |
if ($m_old ne "") { | |
print STDERR "closing $m_old .."; | |
close($fd); | |
print STDERR " ok\n"; | |
} | |
if ($m eq "Jan") { | |
$y++; | |
} | |
$fn = "./nagios_${y}-${mon{$m}}"; | |
print STDERR "creating new $fn .."; | |
die "already exists" if (-e $fn); | |
open($fd, ">", $fn) || die "Error: $."; | |
print STDERR " $y ok\n"; | |
$m_old = $m; | |
} | |
print $fd $_; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Time to split 13GB and 85 million lines into 77 files on Linux 2.6 with 2 core 2.13GHz Xeon and 1GB memory:
real 19m55.749s
user 3m41.751s
sys 0m45.012s