[求助]Shell编写apache日志分析脚本
日志格式如下:
127.0.0.1 - - [14/Feb/2012:00:02:32 +0800] "GET /port/user.php?uid=54129&cla=baidu&key=e5d38743014f88f953269399282c86f5 HTTP/1.1" 200 4 "-" "-" 35622
127.0.0.1 - - [14/Feb/2012:00:02:32 +0800] "GET /test.php HTTP/1.1" 200 4 "-" "-" 35622
127.0.0.1 - - [14/Feb/2012:00:02:32 +0800] "GET /port/user.php?uid=12312&cla=google&key=e5d38743014f88f953269399282c86f5 HTTP/1.1" 200 4 "-" "-" 35622
127.0.0.1 - - [14/Feb/2012:00:02:32 +0800] "GET /port/port.php?uid=15642&key=e5d38743014f88f953269399282c86f5 HTTP/1.1" 200 4 "-" "-" 35622
127.0.0.1 - - [14/Feb/2012:00:02:32 +0800] "GET /test.php HTTP/1.1" 200 4 "-" "-" 35622
需求如下:
user.php的分析结果文件名从这里来……aaa.php?cla=bbb……
>>> aaa.bbb.log。
port.php的分析结果输出就直接是port.log
内容格式如下:
IP date response
127.0.0.1 14/Feb/2012:00:02:32 35622
只要user.php和port.php的访问日志。
默认情况下筛选user\port的所有日志。
另外,可以在执行脚本时指定user或port,以及某一天的日期进行筛选。
=========================
以下是我大概写的片断,菜鸟,请见谅。
若有更好的也请前辈位帮忙改写一下,谢谢!
……path=/apache/access/log/host=$1case $type in"user")cat $file | grep $type | awk '/GET/&&/user.php\?uid=/{split($7,a,"[/|.|?|=|&]");print $1,substr($4,2),$NF > a[3]"."a[8]".log"}';;"port")cat $file | grep $type | awk -F " " '{print $1,substr($4,2),$NF}' >> $path$type.log # 这里的$path起作用了;;esac……
[16:59:50] tty:[2] jobs:[0] cwd:[~/shell][liangdong@jx-csblog01.jx.baidu.com shell]$ echo -e "a.b.c\nd.e.c\n" | awk -F '.' '{print $1"."$2 >> $3".log"}'[17:01:25] tty:[2] jobs:[0] cwd:[~/shell][liangdong@jx-csblog01.jx.baidu.com shell]$ lltotal 16-rw-rw-r-- 1 liangdong liangdong 80 Feb 16 13:08 a.txt-rw-rw-r-- 1 liangdong liangdong 30 Feb 16 13:11 b.txt-rw-rw-r-- 1 liangdong liangdong 8 Feb 20 17:01 c.log-rwxrw-r-- 1 liangdong liangdong 14 Feb 20 16:23 test.sh[17:01:26] tty:[2] jobs:[0] cwd:[~/shell][liangdong@jx-csblog01.jx.baidu.com shell]$ cat c.log a.bd.e
[解决办法]
awk里的变量不需要$,没有其他错误.