Skip to content

Instantly share code, notes, and snippets.

@varp
Created April 23, 2015 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save varp/8ed92c76370944f66468 to your computer and use it in GitHub Desktop.
Save varp/8ed92c76370944f66468 to your computer and use it in GitHub Desktop.
Split apache access log file
require 'date'
apache_log = ARGV[0] unless ARGV.empty?
@cur_date = nil
@next_date = nil
File.open(apache_log) do |f|
for line in f
if line.match /(\d{1,2}\/\w{1,3}\/\d{1,4})/
@next_date = Date.strptime($~[1], "%d/%b/%Y").strftime("%d-%m-%Y")
unless @cur_date == @next_date
@cur_date = @next_date
begin
log.close
rescue NameError
log = nil
end
log = File.new "access-#{@cur_date}.log", "w"
else
log.write(line)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment