关闭
Hit
enter
to search or
ESC
to close
May I Suggest ?
#leanote #leanote blog #code #hello world
Okeeper's Blog
Home
Archives
Tags
DevOps
软件笔记
Spring
学习
JVM系列
关于我
Linux 查找日志常用命令和技巧
无
3196
0
0
zhangyue
#1. tail & head ``` #查询最后100行日志 tail -n 100 info.log 查询从100行之后的所有日志 tail -n +100 info.log 查询日志文件中的头10行日志; head -n 10 info.log 查询日志文件除了最后10行的其他所有日志; head -n -10 info.log ``` #2. 场景一:查找关键字附近的日志 ##1 .获取关键字的行号 ``` #-n 显示行号 cat -n test.log |grep "关键字" ```  ##2. 查找指定行号`63829`附近的日志 ``` cat -n test.log |tail -n +63820|head -n 20 `tail -n +63820表示查询63820行之后的日志` `head -n 20 则表示在前面的查询结果里再查前20条记录` ``` #3. 场景二:根据时间查找日志 ``` #查找2017-07-03 21:21 ~ 2017-07-03 21:22内的日志 sed -n '/2017-07-03 21:21/,/2017-07-03 21:22/p' info.log #查找2017-07-03 21:21:34 ~ 2017-07-03 21:21:39内的所有日志 sed -n '/2017-07-03 21:21:34/,/2017-07-03 21:21:39/p' info.log ``` > 关于日期打印,可以先 grep '2014-12-17 16:17:20' test.log 来确定日志中是否有该时间点,以确保第4步可以拿到日志 这个根据时间段查询日志是非常有用的命令. #4. 场景三:如果我们查找的日志很多,打印在屏幕上不方便查看, 有两个方法 ## 使用more和less命令, 如: `cat -n info.log |grep "地形" |more` 这样就分页打印了,通过点击空格键翻页 ## 使用 `>xxx.txt` 将其保存到文件中,到时可以拉下这个文件分析.如: ``` cat -n info.log |grep "地形" >xxx.txt ```
觉得不错,点个赞?
Please enable JavaScript to view the
comments powered by Disqus.
comments powered by
Disqus
文章目录