一、基本查找命令
1.查詢日志尾部最后10行的日志
tail -n 10 /opt/esage/tomcat/logs/api.log
2.查詢10行之后的所有日志
tail -n +10 /opt/esage/tomcat/logs/api.log
3.查詢日志文件中的頭10行日志
head -n +10 /opt/esage/tomcat/logs/api.log
二、組合查詢
1.先按關鍵字確定關鍵行號
cat -n /opt/esage/tomcat/logs/api.log | grep '關鍵字'
例如:error
cat -n /opt/esage/tomcat/logs/api.log | grep 'error'
2.得到關鍵行號為48470,查詢48470前后40行的日志
cat -n /opt/esage/tomcat/logs/api.log |tail -n +48470 |head -n 40
tail -n +48470 查詢48470之后的所有日志
head -n 40 在之前的結果上查詢前40條日志
那么按日期怎么查呢? 通常我們非常需要查找指定時間端的日志
sed -n '/2014-12-17 16:17:20/,/2014-12-17 16:17:36/p' test.log
特別說明:上面的兩個日期必須是日志中打印出來的日志,否則無效.