1. 程式人生 > >Linux 文件日誌篩選操作

Linux 文件日誌篩選操作

shel hit 排列 定時 ace grep mon int sta

統計查看文件以及篩選日誌

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 5、shell統計一天 access.log 日誌每小時每IP訪問次數 : awk -vFS="[:]" ‘{gsub("-.*","",$1);num[$2" "$1]++}END{for(i in num)print i,num[i]}‘ logs/access.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 "地形" >xxx.txt

Linux 文件日誌篩選操作