转自: http://www.javashuo.com/article/p-ddqgjgrg-kh.htmlhtml
---------------------------------------------------------------------------------------------------------------------------------less
1.查看日志经常使用命令debug
tail: 日志
-n 是显示行号;至关于nl命令;例子以下:htm
tail -100f test.log 实时监控100行日志blog
tail -n 10 test.log 查询日志尾部最后10行的日志;get
tail -n +10 test.log 查询10行以后的全部日志;test
head: 监控
跟tail是相反的,tail是看后多少行日志;例子以下:sed
head -n 10 test.log 查询日志文件中的头10行日志;
head -n -10 test.log 查询日志文件除了最后10行的其余全部日志;
cat:
tac是倒序查看,是cat单词反写;例子以下:
cat -n test.log |grep "debug" 查询关键字的日志
2. 应用场景一:按行号查看---过滤出关键字附近的日志
1)cat -n test.log |grep "debug" 获得关键日志的行号
2)cat -n test.log |tail -n +92|head -n 20 选择关键字所在的中间一行. 而后查看这个关键字前10行和后10行的日志:
tail -n +92表示查询92行以后的日志
head -n 20 则表示在前面的查询结果里再查前20条记录
3. 应用场景二:根据日期查询日志
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 来肯定日志中是否有该 时间点
4.应用场景三:日志内容特别多,打印在屏幕上不方便查看
(1)使用more和less命令,
如: cat -n test.log |grep "debug" |more 这样就分页打印了,经过点击空格键翻页
(2)使用 >xxx.txt 将其保存到文件中,到时能够拉下这个文件分析
如:cat -n test.log |grep "debug" >debug.txt