• linux日志查看技巧


    1、常用查看命令

    # 查看实时日志并过滤
    tail -f test.log |grep -a '关键字'
    
    # 过滤关键字数据,追加到文件
    cat -n test.log | grep "关键字" > error.log
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、 按行号查看—过滤出关键字附近的日志

    因为通常时候我们用grep拿到的日志很少,我们需要查看附近的日志.

    我是这样做的,首先得到关键日志的行号

    cat -n test.log | grep "地形"  
    
    • 1

    如得到"地形"关键字所在的行号是102行. 此时如果我想查看这个关键字前10行和后10行的日志:

    cat -n test.log |tail -n +92|head -n 20
    
    • 1

    tail -n +92表示查询92行之后的日志

    head -n 20则表示在前面的查询结果里再查前20条记录

    3、查看指定时间段内的日志

    sed -n '/2017-02-23 10:00:00/,/2017-02-23 10:06:23/p' test.log 
    sed -n '/2017-02-23 10:00:/,/2017-02-23 10:06:/p' test.log
    
    • 1
    • 2

    这个需要注意的地方是10:00:00或者10:00必须出现过.
    如果没有任何日志出现.请先查看是否存在

    grep "2017-02-23 10:00" test.log
    
    • 1

    4、查看日志中特定字符的匹配数目

    grep "abcd" test.log | wc -l
    
    • 1

    image-20220914152918651

    5、实时查看日志

    tail -f test.log
    
    tail -f test.log |grep -a '关键字'
    
    • 1
    • 2
    • 3

    6、查询最后20行,并查找关键字“结果”

    tail -n 20 test.log | grep '结果'
    # 查询最后20行,并查找关键字“结果”,文字标红
    tail -n 20 test.log | grep '结果' --color
    # 查询最后20行,并查找关键字“结果”,文字标红,上下扩展两行
    tail -n 20 test.log | grep '结果' --color -a2
    
    • 1
    • 2
    • 3
    • 4
    • 5

    7、分页查看,使用空格翻页(使用more/less)

    cat -n test.log | grep "error" | more
    
    • 1

    8、日志文件超大时,使用vim查找

    vim test.log 打开文件

    ctrl + g 移动至文件末尾

    :? com.fm.fdata 从最后往上查找关键字,点N往上

  • 相关阅读:
    prometheus学习2数据类型了解&PromQL
    word字间距突然变大怎么办?
    Mysql知识复习二
    C++算法:利用拓扑排序解决戳印序列
    flutter 合并数组及操作符
    多功能气象传感器解析
    Git的安装与环境配置
    RabbitMQ初步到精通-第二章-RabbitMQ介绍
    Java面试题:细数ThreadLocal大坑,内存泄露本可避免
    STM32(TIM定时器中断)
  • 原文地址:https://blog.csdn.net/qq_16116881/article/details/126855385