shell练习题-统计日志

1.要求:

统计出/test目录下,每天生成的日志文件的每个IP的访问量有多少?
将访问量存放到/test1下的文件中,并将大于七天的日志文件与存放访问量的文件删除。
(日志格式例:2018-06-1-access-log.txt)
(存放的文件格式例:2018-06-1-log.txt)


日志内容:

[root@liang test]# head -5 2018-06-12-access-log.txt 
115.239.212.7 - - [11/May/2016:13:50:01 -0400] "GET /assets/css/common.css HTTP/1.1" 200 11308 "http://www.1daoyun.com/course/prolist/1" "Baidu-YunGuanCe-ScanBot(ce.baidu.com)" "10.214.61.39, 10.195.51.33, 127.0.0.1"
115.239.212.7 - - [11/May/2016:13:50:01 -0400] "GET /assets/css/theme-colors.css HTTP/1.1" 200 9256 "http://www.1daoyun.com/course/prolist/1" "Baidu-YunGuanCe-ScanBot(ce.baidu.com)" "10.214.61.39, 10.195.50.33, 127.0.0.1"
180.97.106.36 - - [11/May/2016:13:50:01 -0400] "GET /assets/plugins/bootstrap/fonts/glyphicons-halflings-regular.ttf HTTP/1.0" 200 45404 "http://www.1daoyun.com/course/prolist/1" "Baidu-YunGuanCe-ScanBot(ce.baidu.com)" "-"
180.97.106.36 - - [11/May/2016:13:50:02 -0400] "GET /course/explore HTTP/1.0" 404 0 "-" "Baidu-YunGuanCe-ScanBot(ce.baidu.com)" "-"
180.97.106.36 - - [11/May/2016:13:50:03 -0400] "GET /assets/plugins/bootstrap/fonts/glyphicons-halflings-regular.ttf HTTP/1.0" 200 45404 "-" "Baidu-YunGuanCe-ScanBot(ce.baidu.com)" "-"
2.模拟环境:

1.不知道博客怎么上传文件,所以不提供日志文件,可以在网上搜索,然后修改日志文件名字为当天的时间(2018-06-12-access-log.txt),并移动到/test目录下


2.再生成十一个文件,进行测试。(这些文件都是空的)

[root@liang test]# for i in `seq 1 11`;do touch 2018-06-${i}-access-log.txt;done 


[root@liang test]# ll
total 11264
-rw-r--r--. 1 root root        0 Jun 12 08:07 2018-06-1-access-log.txt
-rw-r--r--. 1 root root        0 Jun 12 08:07 2018-06-2-access-log.txt
-rw-r--r--. 1 root root        0 Jun 12 08:07 2018-06-3-access-log.txt
-rw-r--r--. 1 root root        0 Jun 12 08:07 2018-06-4-access-log.txt
-rw-r--r--. 1 root root        0 Jun 12 08:07 2018-06-5-access-log.txt
-rw-r--r--. 1 root root        0 Jun 12 08:07 2018-06-6-access-log.txt
-rw-r--r--. 1 root root        0 Jun 12 08:07 2018-06-7-access-log.txt
-rw-r--r--. 1 root root        0 Jun 12 08:07 2018-06-8-access-log.txt

-rw-r--r--. 1 root root        0 Jun 12 08:07 2018-06-9-access-log.txt

-rw-r--r--. 1 root root        0 Jun 12 08:07 2018-06-10-access-log.txt

-rw-r--r--. 1 root root        0 Jun 12 08:07 2018-06-11-access-log.txt

-rw-r--r--. 1 root root 11526717 Jun 12 08:21 2018-06-12-access-log.txt

3.脚本答案:

[root@liang 2018-06-12]# cat account.sh 
#!/bin/bash
Date=`date +%F`
File=/test/${Date}-access-log.txt
if [ -f "$File" ];then
        /usr/bin/awk '{print $1}' ${File} |sort|uniq -c|sort -nr >/test1/${Date}-log.txt
else
        echo "Error:No $File file "
fi
find /test -type f -mtime +7|xargs rm -r

find /test1 -type f -mtime +7|xargs rm -r

4.定时任务

[root@liang 2018-06-12]# crontab -l
#count log ip traffic
00 00 * * * /usr/bin/bash /scritp/2018-06-12/account.sh

5.uniq与sort命令

#sort---排序,-n 按数值排序,-r 倒叙排序,-u 去掉重复行,-k1 指定列数1.

#uniq---去除连续的重复行,一般与sort配合使用,-c 在每列的旁边显示该行重复出现的次数。


1.例:

[root@liang test]# cat a 
aa
aa
aa
bb
cc
dd
ee
bb

cc

2.去除连续的重复行

[root@liang test]# uniq a
aa (三行连续的aa,会有两行被去掉)
bb
cc
dd
ee
bb

cc

3.显示该行重复出现的次数

[root@liang test]# uniq -c a
      3 aa
      1 bb
      1 cc
      1 dd
      1 ee
      1 bb

      1 cc


4.统计次数
[root@liang test]# uniq -c a|sort -ur
      3 aa
      1 ee
      1 dd
      1 cc
      1 bb


猜你喜欢

转载自blog.csdn.net/liang_operations/article/details/80665934