統計查看文件以及篩選日志
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