如何分割按月现有的Apache日志文件? [英] How to split existing apache logfile by month?

查看:198
本文介绍了如何分割按月现有的Apache日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

怎么能够斯普利特一个月现有的Apache日志文件到单独的文件?

How can one split existing apache logfiles into separate files by month?

我已经冲刷网页,我无法找到任何东西。是的,我知道logrotate的和的cronolog和所有。但没有我发现帮助我以极快的现有文件。

I've scoured the web and I can't find anything. Yes, I know about logrotate and cronolog and all that. But nothing I've found helps me with splitting existing files.

是否有一个awk脚本什么的?

Is there an awk script or something?

下面是数据的一个片段:

Here's a snippet of the data:

124.115.5.11 - - [30/May/2011:23:21:37 -0500] "GET / HTTP/1.0" 200 206492 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322;TencentTraveler)"
58.61.164.39 - - [31/May/2011:00:36:35 -0500] "GET / HTTP/1.0" 200 206492 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322;TencentTraveler)"
114.80.93.55 - - [31/May/2011:01:42:17 -0500] "GET / HTTP/1.0" 200 206492 "-" "Sosospider+(+http://help.soso.com/webspider.htm)"
114.80.93.73 - - [31/May/2011:02:03:44 -0500] "GET / HTTP/1.0" 200 206492 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322;TencentTraveler)"
123.125.71.98 - - [31/May/2011:12:33:30 -0500] "GET / HTTP/1.1" 103 24576 "-" "Baiduspider+(+http://www.baidu.com/search/spider.htm)"
220.181.108.187 - - [31/May/2011:12:33:55 -0500] "GET / HTTP/1.1" 103 24576 "-" "Baiduspider+(+http://www.baidu.com/search/spider.htm)"
123.125.71.117 - - [31/May/2011:13:27:56 -0500] "GET / HTTP/1.1" 103 24576 "-" "Baiduspider+(+http://www.baidu.com/search/spider.htm)"
123.125.71.78 - - [31/May/2011:16:45:48 -0500] "GET /node/54 HTTP/1.1" 200 3219 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"
124.115.1.8 - - [31/May/2011:19:59:58 -0500] "GET / HTTP/1.1" 200 206492 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
123.125.71.69 - - [31/May/2011:22:05:46 -0500] "GET / HTTP/1.1" 200 206492 "-" "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"


下面是我的解决方案,通过下面的史蒂夫的答案极大地鼓舞了:


Here's my solution, greatly inspired by Steve's answer below:

一个方法使用 AWK

awk 'BEGIN {
    split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ", months, " ")
    for (a = 1; a <= 12; a++)
        m[months[a]] = a
}
{
    year = array[3]
    month = sprintf("%02d", m[array[2]])

    split($4,array,"[:/]");
    print > FILENAME"-"year"_"month".txt"
}' incendiary.ws-2009

这将输出文件如:

incendiary.ws-2010-2010_04.txt
incendiary.ws-2010-2010_05.txt
incendiary.ws-2010-2010_06.txt
incendiary.ws-2010-2010_07.txt

针对一个150 MB的日志文件,通过chepner公认的答案了<强70秒。在一个3.4 GHz的8核Xeon E31270,而这种方法花费的5秒

Against a 150 MB log file, the Accepted Answer by chepner took 70 seconds on an 3.4 GHz 8 Core Xeon E31270, while this method took 5 seconds.

最初灵感: http://stackoverflow.com/a/11714105/430062

推荐答案

使用的一种方法 AWK

awk '{ split($4,array,"/"); print > array[2] ".txt" }' file.txt

这将输出文件如:

May.txt
June.txt
July.txt
etc

编辑:

也许你想保持独立的岁月:

Perhaps you would like to keep the years separate:

awk '{ split($4,array,"[:/]"); print > array[2] array[3] ".txt" }' file.txt

这将输出文件如:

May2011.txt
May2012.txt
July2011.txt
etc

这篇关于如何分割按月现有的Apache日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆