linux通过grep根据关键字查找日志文件上下文

1、在标准unix/linux下的grep命令中,通过以下参数控制上下文的显示:

grep -C 10 keyword catalina.out 显示file文件中匹配keyword字串那行以及上下10行

grep -B 10 keyword catalina.out 显示keyword及前10行

grep -A 10 keyword catalina.out 显示keyword及后10行

2、查找的结果比较大时,为了方便定位问题,也可以重定向到文件中,比如:

grep -C 10 keyword catalina.out  > aaa.txt

3、统计包含某个关键字的个数

grep -o keyword catalina.out | wc -l

4、如果一行最多一个关键字,可以简写为:
   grep -c keyword catalina.out

5、查看grep版本的方法是

grep -V

Grep命令详细说明:

Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c

Regexp selection and interpretation:
  -E, --extended-regexp     PATTERN is an extended regular expression (ERE)
  -F, --fixed-strings       PATTERN is a set of newline-separated fixed strings
  -G, --basic-regexp        PATTERN is a basic regular expression (BRE)
  -P, --perl-regexp         PATTERN is a Perl regular expression
  -e, --regexp=PATTERN      use PATTERN for matching
  -f, --file=FILE           obtain PATTERN from FILE
  -i, --ignore-case         ignore case distinctions
  -w, --word-regexp         force PATTERN to match only whole words
  -x, --line-regexp         force PATTERN to match only whole lines
  -z, --null-data           a data line ends in 0 byte, not newline

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             display version information and exit
      --help                display this help text and exit

Output control:
  -m, --max-count=NUM       stop after NUM matches
  -b, --byte-offset         print the byte offset with output lines
  -n, --line-number         print line number with output lines
      --line-buffered       flush output on every line
  -H, --with-filename       print the file name for each match
  -h, --no-filename         suppress the file name prefix on output
      --label=LABEL         use LABEL as the standard input file name prefix
  -o, --only-matching       show only the part of a line matching PATTERN
  -q, --quiet, --silent     suppress all normal output
      --binary-files=TYPE   assume that binary files are TYPE;
                            TYPE is 'binary', 'text', or 'without-match'
  -a, --text                equivalent to --binary-files=text
  -I                        equivalent to --binary-files=without-match
  -d, --directories=ACTION  how to handle directories;
                            ACTION is 'read', 'recurse', or 'skip'
  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;
                            ACTION is 'read' or 'skip'
  -r, --recursive           like --directories=recurse
  -R, --dereference-recursive
                            likewise, but follow all symlinks
      --include=FILE_PATTERN
                            search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN
                            skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN directories that match PATTERN will be skipped.
  -L, --files-without-match print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count               print only a count of matching lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM
      --group-separator=SEP use SEP as a group separator
      --no-group-separator  use empty string as a group separator
      --color[=WHEN],
      --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is 'always', 'never', or 'auto'
  -U, --binary              do not strip CR characters at EOL (MSDOS/Windows)
  -u, --unix-byte-offsets   report offsets as if CRs were not there
                            (MSDOS/Windows)

'egrep' means 'grep -E'.  'fgrep' means 'grep -F'.
Direct invocation as either 'egrep' or 'fgrep' is deprecated.
When FILE is -, read standard input.  With no FILE, read . if a command-line
-r is given, - otherwise.  If fewer than two FILEs are given, assume -h.
Exit status is 0 if any line is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.

Report bugs to: bug-grep@gnu.org
GNU Grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <http://www.gnu.org/gethelp/>

linux通过grep根据关键字查找日志文件上下文的更多相关文章

  1. Linux显示以log结尾的日志文件

    Linux显示以log结尾的日志文件 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ find -name "*.log" find: `. ...

  2. Linux 查看磁盘容量、查找大文件、查找大目录

    Linux 查看磁盘容量.查找大文件.查找大目录 磁盘统计 查看磁盘使用情况 df -h 文件统计 查找/home 目录下大于800M的文件 find /home -type f -size +800 ...

  3. LINUX中,find结合grep正则表达式,快速查找代码文件。

    ###目的###LINUX中,find结合grep正则表达式快速查找代码. 例如经常有需求:查找当前目录下所有.h文件中,"public开头,中间任意字符,以VideoFrameReceiv ...

  4. linux之使用cron,logrotate管理日志文件

    1) logrotate配置   logrotate 程序是一个日志文件管理工具.用来把旧的日志文件删除,并创建新的日志文件,我们把它叫做“转储”.   我们可以根据日志文件的大小,也可以根据其天数来 ...

  5. Linux批量清空当前目录中的日志文件

    在Linux中,有时需要批量清空当前目录中的日志文件,同时还要保留日志文件. 其实一行shell命令就可以搞定,一起来看看吧. 在当前目录下,键入如下命令: for i in `find . -nam ...

  6. linux使用grep和find查找内容

    1,在某个路径下查文件. 在/etc下查找“*.log”的文件 find /etc -name “*.log” 2,扩展,列出某个路径下所有文件,包括子目录. find /etc -name “*” ...

  7. Default Trace 查找日志文件快速增长的原因

    select loginname, loginsid, spid, hostname, applicationname, servername, databasename, objectname, e ...

  8. linux后台启动springboot并指定日志文件名称

    如果用nohup启动jar包的话,默认的日志文件就是nohup.out,那么如果启动多个jar包的话,看日志文件就麻烦了,因为他们都会写入到nohup.out文件中. 所以我们来指定一下不同jar包的 ...

  9. Linux /var/log下的各种日志文件详解

    1)/var/log/secure:记录登录系统存取数据的文件;例如:pop3,ssh,telnet,ftp等都会记录在此. 2)/var/log/wtmp:记录登录这的信息记录,被编码过,所以必须以 ...

随机推荐

  1. maxima安装&使用

    环境: mint 19 或者 > ubuntu 18 源代码安装的好处, 可以使用最新版. mint 19.1 下面, 利用apt 直接安装的maxima版本太老,不支持 draw 函数. lo ...

  2. Django1.11基础视图

    Django视图 路由命名与reverse反解析 在项目urls中的include函数,使用namespace参数定义路由命名空间 url(r'^',incude('book.urls',namesp ...

  3. 18 11 13 装了ssd 继续 网络通信 tcp 客户端的创建

    import socket def main(): # 1. 买个手机(创建套接字 socket) 联通公司建立了一个信号塔 tcp_server_socket = socket.socket(soc ...

  4. mysql安装完之后,登陆后发现只有两个数据库

    mysql安装完之后,登陆后发现只有两个数据库:mysql> show databases;+--------------------+| Database           |+------ ...

  5. 三大PLM厂商

    西门子的叫Teamcenter(汽车.通用机械) 法国达索的Enovia(航空\汽车\高铁等交通运输行业和机械行业) PTC的叫windchill(船舶.电子)

  6. MVC思想概叙

    随着应用系统的逐渐增大,系统的业务逻辑复杂度是以几何的方式增长,在这种情况下,如果依然把所有的业务逻辑都放在JSP页面中,那将成为一场恶梦. MVC思想将应用中各个组件按照功能来进行分类,不同的组将使 ...

  7. JDK8新特性之stream

    stream中有很多方法,讲一些常用的. 1.forEach(),遍历方法,很简单,对于一般的遍历可以替代for循环 List<String> strings = Arrays.asLis ...

  8. 1. Ruby基础知识

    1. Ruby执行选项 符号 作用 -c 检查代码正确性 -w 警告模式运行 -e 字面脚本 -l 行模式运行 单独 ruby -c Hello.rb 组合 ruby -le 'print " ...

  9. 单个body|简单解释|复杂解释|反面解释

    单个body有三种方法简单解释.复杂解释和反面解释 ========================================================================== ...

  10. LAMP环境搭建,防火墙开启,数据库挂载在逻辑卷

    具体要求: 1. 源码部署 LAMP 环境, 和生产保持一致 2. 静态资源文件同步生产环境 3. 需要同时部署 2 个 web 网站 步骤: 一. 需要的安装包. 提前准备好. apr-util-1 ...