当前位置:首页 > 操作系统 > Linux

常用linux命令----查日志(2)

查日志的基本命令

  1. tail
tail -f catalina.out		   //实时监控文件输出
tail -n 10 test.log            // 查看test.log  最后10行日志
tail -n +10 test.log		   // 查看test.log  从第10行之后的所有日志
tail -f -n 100 catalina.out    // 打印当前最后100行,并实时刷新日志(1)
tail -f catalina.out -n 100    // 打印当前最后100行,并实时刷新日志(2)
tail -f -n +100 catalina.out   // 打印第100行之后的所有日志,并实时刷新(1)
tail -f catalina.out -n +100   // 打印第100行之后的所有日志,并实时刷新(2)
  1. head
head catalina.out -n 100       // 查看前面100行日志记录
  1. cat
cat -n test.log | grep "关键词"                       // 查看到关键词相关日志及行号
cat -n test.log | grep "关键词" | more                // 分页显示,按空格键可翻页
cat test.log | tail -n +200 | head -n 100            // 从200开始,显示200行到299行的日志记录
  1. tac(反向列示)

tac 是将 cat 反写过来,所以他的功能就跟 cat 相反,cat 是由第一行到最后一行连续显示在萤幕上,而 tac 则是由最后一行到第一行反向在萤幕上显示出来!

  1. sed
sed -n '200,299p' test.log                                           // 从200开始,显示200行到299行的日志记录
sed -n '/2017-10-11 00:00:00/,/2017-10-11 01:23:23/p' test.log       // 查看某一时间段内的日志记录(两个日期必须在日志中存在,不然会是失效)
  1. grep
grep "关键词" test.log --color=auto				      // 查询文件中关键词所在行,并把关键词高亮显示
grep "关键词" -A10 -B20 test.log                      // 查询文件中关键词所在行,及前20行后10行的
tail -f catalina.out | grep "关键词" --color=auto     // 实时日志记录中,过滤只显示包含关键词的日志,并将关键词设置高亮(1)
tail -f catalina.out | grep --color=auto -i "关键词"  // 实时日志记录中,将关键词设置高亮(2)
tail -f catalina.out | grep --color=auto "关键词"     // 实时日志记录中,将关键词设置高亮(3)
tail -f catalina.out | grep -v "关键词"               // 反向查找,查询实时日志中不包含关键词的行的
tail -f catalina.out | perl -pe 's/(关键词)/e[1;31m$1e[0m/g'  // 实时日志记录中,将“关键词”设置高亮(3)

Linux下打开超大文件方法

在Linux下用VIM打开大小几个G、甚至几十个G的文件时,是非常慢的。

这时,我们可以利用下面的方法分割文件,然后再打开。

1 查看文件的前多少行
head -10000 /var/lib/mysql/slowquery.log > temp.log
上面命令的意思是:把slowquery.log文件前10000行的数据写入到temp.log文件中。

2 查看文件的后多少行
tail -10000 /var/lib/mysql/slowquery.log > temp.log
上面命令的意思是:把slowquery.log文件后10000行的数据写入到temp.log文件中。

3 查看文件的几行到几行
sed -n ‘10,10000p’ /var/lib/mysql/slowquery.log > temp.log
上面命令的意思是:把slowquery.log文件第10到10000行的数据写入到temp.log文件中。

4 根据查询条件导出 cat catalina.log | grep ‘2017-09-06 15:15:42’ > test.log

查看命令实战场景

1.查看日志常用命令
   

     tail:  
          	   -n  是显示行号;相当于nl命令;例子如下:
                tail -100f test.log      实时监控100行日志
                tail  -n  10  test.log   查询日志尾部最后10行的日志;
    
                tail -n +10 test.log    查询10行之后的所有日志;
    
    head:  

        跟tail是相反的,tail是看后多少行日志;例子如下:

            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
          
            
  5.应用场景四:查看某段时间内的关键字日志
  
     sed -n ‘/2018-06-21 14:30:20/,/2018-06-21 16:12:00/p’ catalina.out |grep ‘keyword’

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐