zoukankan      html  css  js  c++  java
  • linux系统根据关键字查看日志

    假设存在日志文件 hrun.log,查询的关键字为"新增用户":

    根据关键字查看日志
    cat hrun.log | grep "新增用户"

    根据关键字查看后10行日志
    cat hrun.log | grep "新增用户" -A 10

    根据关键字查看前10行日志
    cat hrun.log | grep "新增用户" -B 10

    根据关键字查看前后10行日志,并显示出行号
    cat -n hrun.log | grep "新增用户" -C 10

    查看日志前 50 行
    cat hrun.log | head -n 50

    查看日志后 50 行,并显示出行号
    cat -n hrun.log | tail -n 50

    说明:

        -A 表示关键字之后,After
        -B 表示关键字之前,Before
        -C 表示关键字前后,Context


    linux之grep命令

    grep是一个多用途的文本搜索工具,linux中使用非常频繁,并且使用很灵活,可以是变量,也可以是字符串。最基本的用法有以下两种:
    1.搜索内容中无空格,可以直接执行grep命令,比如:grep pass a.txt,表示在a.txt文件中搜索pass所在的行。
    2.如果搜索内容中有空格,则需要使用单引号或者双引号把搜素内容引起来,比如:
    grep "hello all" a.txt或者grep 'hello all' a.txt,如果不加单双引号,则提示搜搜错误,无法识别,因为不加引号,直接grep hello all a.txt,表示在all 和a.txt中搜索hello,这肯定是不对的

    grep的一些参数命令:
    1. grep -c option file:显示文件中包含搜索内容行数,比如前面的命令表示显示 file中包含option内容的行数是几
    2. grep -n option flie:列出所有的匹配行,并在最前面添加行的序列数
    3. grep -v option file:显示文件中不包含所搜索内容的行数,这个和-c的参数正好相反
    4. grep -i option file:列出所搜索内容的匹配行,搜索过程中不区分大小写
    5. grep -l option *:列出所有包含option内容的文件的名
    6. grep -r option :对当前目录和所有的子目录进行搜索
    7. grep -w option file:精确搜索,可以说准确性搜索,比如:grep -w b* a.txt:此命令执行时,*不会默认为任何字符,只表示字面意思,就是一个*字符.
    8. grep -x option file:完全匹配输出,比如:grep -x hello a.txt,只会输出某一行存在hello字符串,并且此行仅包含hello的内容。假设a.txt中有一行“hello all”,执行上述命令,此行不会被搜索到。

  • 相关阅读:
    bzoj1711
    bzoj1458
    bzoj1433
    hdu2732
    bzoj1066
    hdu3549
    poj1698
    [ZJOI2007]时态同步
    SA 学习笔记
    [LUOGU]2016 Sam数
  • 原文地址:https://www.cnblogs.com/peiya/p/12622573.html
Copyright © 2011-2022 走看看