Linux 文件对日志筛选操作

统计查看文件以及筛选日志

1、*.log 日志文件中 统计独立ip的个数:
awk '{print $1}' test.log | sort | uniq | wc -l
2、查询访问最多的前10个ip
awk '{print $1}' /access.log  | sort | uniq -c | sort -nr | head -10
3、查看某段时间的
grep "2017:0[3-6]" test.log 
4、访问次数最多的IP
netstat -ntu | tail -n +3 | awk '{ print $5}' | cut -d : -f 1 | sort | uniq -c| sort -n -r | head -n 5
tail -n +3 :去掉前两行。
awk '{ print $5}':取数据的低5域(第5列)
cut -d : -f 1 :取IP部分。
sort:对IP部分进行排序。
uniq -c:打印每一重复行出现的次数。(并去掉重复行)
sort -n -r:按照重复行出现的次序倒序排列。
head -n 5:取排在前5位的IP
5、shell统计一天 access.log 日志每小时每IP访问次数 :
awk -vFS="[:]" '{gsub("-.*","",$1);num[$2" "$1]++}END{for(i in num)print i,num[i]}' test.log
6、筛选关键字
grep 
grep '01/Sep/2017:16:06:47' logs/access.log cat /opt/mongodb/log/mongodb.log.2016-12-10T05-36-42 |grep "Dec 10"

sed
sed -n '/Dec 10/p' /opt/mongod/log/mongod.log
 
awk
awk '/Dec 10/ {print $0}' /opt/mongod/log/mongod.log
6、查询具体时间点 日志;
sed
sed -n '/Nov  11 16:24:17/p' /var/log/secure

awk
awk '/Nov  11 16:24:17/ {print $0}' /var/log/secure

tail -n  10  test.log   查询日志尾部最后10行的日志;
tail -n +10 test.log    查询10行之后的所有日志;
head -n  10  test.log   查询日志文件中的头10行日志;
head -n -10  test.log   查询日志文件除了最后10行的其他所有日志;

cat -n test.log |tail -n +92|head -n 20
tail -n +92    表示查询92行之后的日志
head -n 20    则表示在前面的查询结果里再查前20条记录
7、查找指定时间端的日志
sed -n '/2014-12-17 16:17:20/,/2014-12-17 16:17:36/p'  test.log
grep '2014-12-17 16:17:20' test.log 
8、使用 >xxx.txt 将其保存到文件中,到时可以拉下这个文件分析.如:
cat -n test.log |grep "牛逼"  >test.log
 文件 处理
当前网络链接 排序;
netstat -ntu | tail -n +3 | awk '{ print $5}' | cut -d : -f 1 | sort | uniq -c| sort -n -r | head -n 5
tail -n +3 :去掉前两行。 awk '{ print $5}':取数据的低5域(第5列) cut -d : -f 1 :取IP部分。 sort:对IP部分进行排序。 uniq -c:打印每一重复行出现的次数。(并去掉重复行) sort -n -r:按照重复行出现的次序倒序排列。 head -n 5:取排在前5位的IP
shell 统计一天 access.log 日志每小时每IP访问次数 :
 awk -vFS="[:]" '{gsub("-.*","",$1);num[$2" "$1]++}END{for(i in num)print i,num[i]}' logs/access.log
查询 筛选关键字
grep
grep '01/Sep/2017:16:06:47' logs/access.log cat /opt/mongodb/log/mongodb.log.2016-12-10T05-36-42 |grep "Dec 10"
sed sed
-n '/Dec 10/p' /opt/mongod/log/mongod.log awk awk '/Dec 10/ {print $0}' /opt/mongod/log/mongod.log
筛选 具体时间点 日志;
sed
sed -n '/Nov 11 16:24:17/p' /var/log/secure
sed -n '/2014-12-17 16:17:20/,/2014-12-17 16:17:36/p' test.log
awk
awk
'/Nov 11 16:24:17/ {print $0}' /var/log/secure

grep
grep '2014-12-17 16:17:20' test.log
查询 行数 筛选
tail -n 10 test.log 查询日志尾部最后10行的日志;
tail -n +10 test.log 查询10行之后的所有日志;
head -n 10 test.log 查询日志文件中的头10行日志;
head -n -10 test.log 查询日志文件除了最后10行的其他所有日志;
cat -n test.log |tail -n +92|head -n 20
tail -n +92表示查询92行之后的日志
head -n 20 则表示在前面的查询结果里再查前20条记录

查询 筛选保存 使用 >xxx.txt 将其保存到文件中,到时可以拉下这个文件分析,如:

cat -n test.log |grep "地形" >xxx.txt

 

 

posted @ 2017-11-17 22:05  01234567  阅读(4466)  评论(0编辑  收藏  举报