实时观察日志
tail -f test.log
查询日志尾部最后10行的日志;
tail -n 10 test.log
查询10行之后的所有日志;
tail -n +10 test.log
查询日志文件中的头10行日志;
head -n 10 test.log
查询日志文件除了最后10行的其他所有日志;
head -n -10 test.log
查询某一段记录附近的日志
cat -n test.log |grep “想要查的日志关键词” 得到关键日志的行号
cat -n test.log |tail -n +100|head -n 20
tail -n +100表示查询100行之后的日志
head -n 20 则表示在前面的查询结果里再查前20条记录
按日期查询
关于日期打印,可以先 来确定日志中是否有该时间
grep ‘2020-01-10 20:00:00’ test.log
然后再按时间查找
sed -n ‘/2020-01-09 20:00:00/,/2020-01-10 20:00:00/p’ test.log
其它
(1)使用more和less命令, 如: cat -n test.log |grep “地形” |more 这样就分页打印了,通过点击空格键翻页
(2)使用 >xxx.txt 将其保存到文件中,到时可以拉下这个文件分析.如:
cat -n test.log |grep “关键词” >xxx.txt