前言
任何系统都会出问题,出了问题一般怎么排查BUG?这个时候程序中记录的异常日志以及关键节点的日志就非常重要了,面对一大堆的日志文件,怎么找出我们需要的有用信息呢?linux中可以使用grep命令查找,这个命令的功能非常强大,也是我平时中排查线上错误时使用最多的命令之一。

命令名称
grep

用途
查找指定文件内包含指定关键字(正则表达式)的内容,按行为单位匹配

使用格式
grep [OPTIONS] PATTERN [FILE...]

常用选项
-V (显示命令版本)

正则模式匹配版本
-E, --extended-regexp (Interpret PATTERN as an extended regular expression)
-F, --fixed-strings (Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.)
-G, --basic-regexp (Interpret PATTERN as a basic regular expression (BRE, see below). This is the default.)
-P, --perl-regexp (Interpret PATTERN as a Perl regular expression.)

匹配模式控制
-e PATTERN (Use PATTERN as the pattern. This can be used to specify multiple search patterns.)
-f FILE, --file=FILE (Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing.)
-i, --ignore-case (Ignore case distinctions in both the PATTERN and the input files.)
-v, --invert-match (Invert the sense of matching, to select non-matching lines.)
-x, --line-regexp (整行匹配)

内容输出控制
-c, --count (只显示符合条件的总数量)
--color[=WHEN]
(Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields and groups of context lines)
with escape sequences to display them in color on the terminal. The colors are defined by the environment variable GREP_COLORS. WHEN is never, always, or auto.)
-L, --files-without-match (只输出未匹配的文件名列表 print the name of each input file from which no output would normally have been printed)
-l, --files-with-matches (只输出有匹配的文件名列表)
-m NUM, --max-count=NUM (一个文件只输出符合条件的行数<=NUM)
-o, --only-matching (只输出匹配的部分)
-q, --quiet, --silent (不输出任何内容)
-s, --no-messages (Suppress error messages about nonexistent or unreadable files.)

前缀内容输出控制
-b, --byte-offset
(Print the 0-based byte offset within the input file before each line of output. If -o (--only-matching) is specified, print the offset of the matching part itself.)
-H, --with-filename (输出文件名)
-h, --no-filename (不输出文件名)
--label=LABEL
(Display input actually coming from standard input as input coming from file LABEL. This is especially useful when implementing tools like zgrep, e.g., gzip -cd foo.gz |
grep --label=foo -H something. See also the -H option.)
-n, --line-number (显示行号)
-T, --initial-tab (行中每部分内容以tab结尾,比如:文件名tab行号tab内容)
-u, --unix-byte-offsets
(Report Unix-style byte offsets. This switch causes grep to report byte offsets as if the file were a Unix-style text file, i.e., with CR characters stripped off. This
will produce results identical to running grep on a Unix machine. This option has no effect unless -b option is also used; it has no effect on platforms other than MS-DOS
and MS-Windows.)
-Z, --null
(Output a zero byte (the ASCII NUL character) instead of the character that normally follows a file name. For example, grep -lZ outputs a zero byte after each file name
instead of the usual newline. This option makes the output unambiguous, even in the presence of file names containing unusual characters like newlines. This option can be
used with commands like find -print0, perl -0, sort -z, and xargs -0 to process arbitrary file names, even those that contain newline characters.)

上下文行输出控制
-A NUM, --after-context=NUM
(Print NUM lines of trailing context after matching lines. Places a line containing a group separator (described under --group-separator) between contiguous groups of
matches. With the -o or --only-matching option, this has no effect and a warning is given.)
-B NUM, --before-context=NUM
(Print NUM lines of leading context before matching lines. Places a line containing a group separator (described under --group-separator) between contiguous groups of
matches. With the -o or --only-matching option, this has no effect and a warning is given.)
-C NUM, -NUM, --context=NUM
(Print NUM lines of output context. Places a line containing a group separator (described under --group-separator) between contiguous groups of matches. With the -o or
--only-matching option, this has no effect and a warning is given.)
--group-separator=SEP (Use SEP as a group separator. By default SEP is double hyphen (--).)
--no-group-separator (Use empty string as a group separator.)

文件和目录设置
-a, --text (所有的文件中查找 this is equivalent to the --binary-files=text option.)
--binary-files=TYPE
(If the first few bytes of a file indicate that the file contains binary data, assume that the file is of type TYPE. By default, TYPE is binary, and grep normally outputs
either a one-line message saying that a binary file matches, or no message if there is no match. If TYPE is without-match, grep assumes that a binary file does not match;
this is equivalent to the -I option. If TYPE is text, grep processes a binary file as if it were text; this is equivalent to the -a option. Warning: grep
--binary-files=text might output binary garbage, which can have nasty side effects if the output is a terminal and if the terminal driver interprets some of it as commands.)
-D ACTION, --devices=ACTION
(If an input file is a device, FIFO or socket, use ACTION to process it. By default, ACTION is read, which means that devices are read just as if they were ordinary files.
If ACTION is skip, devices are silently skipped.)
-d ACTION, --directories=ACTION
(If an input file is a directory, use ACTION to process it. By default, ACTION is read, i.e., read directories just as if they were ordinary files. If ACTION is skip,
silently skip directories. If ACTION is recurse, read all files under each directory, recursively, following symbolic links only if they are on the command line. This is
equivalent to the -r option.)
--exclude=GLOB
(Skip files whose base name matches GLOB (using wildcard matching). A file-name glob can use *, ?, and [...] as wildcards, and \ to quote a wildcard or backslash character
literally.)
--exclude-from=FILE
(Skip files whose base name matches any of the file-name globs read from FILE (using wildcard matching as described under --exclude).)
--exclude-dir=DIR (Exclude directories matching the pattern DIR from recursive searches.)
-I (Process a binary file as if it did not contain matching data; this is equivalent to the --binary-files=without-match option.)
--include=GLOB (Search only files whose base name matches GLOB (using wildcard matching as described under --exclude).)
-r, --recursive
(Read all files under each directory, recursively, following symbolic links only if they are on the command line. This is equivalent to the -d recurse option.)
-R, --dereference-recursive (Read all files under each directory, recursively. Follow all symbolic links, unlike -r.)

其它选项
--line-buffered (Use line buffering on output. This can cause a performance penalty.)
-U, --binary
(Treat the file(s) as binary. By default, under MS-DOS and MS-Windows, grep guesses the file type by looking at the contents of the first 32KB read from the file. If grep
decides the file is a text file, it strips the CR characters from the original file contents (to make regular expressions with ^ and $ work correctly). Specifying -U
overrules this guesswork, causing all files to be read and passed to the matching mechanism verbatim; if the file is a text file with CR/LF pairs at the end of each line,
this will cause some regular expressions to fail. This option has no effect on platforms other than MS-DOS and MS-Windows.)
-z, --null-data
(Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null option, this option can be used
with commands like sort -z to process arbitrary file names.)

实践
1.查找某个目录下所有包含keyword1或者keyword2的内容
grep -r -e 'keyword1' -e 'keyword2' $dirname

2.查找包含keyword1且文件名后缀带.log的内容
grep 'keyword1' *.log

3.查找包含keyword1的内容显示带行号
grep -n 'keyword1' $file

4.查找包含keyword1输出内容不显示文件名
grep -h 'keyword1' ./*

后记
有些配置项不是很理解,可能还没找到使用的场景吧,碰到使用的场景时,再好好琢磨琢磨 >_<.

参考资料
【1】man grep

转载于:https://www.cnblogs.com/wadeyu/p/6035518.html

N天学习一个Linux命令之grep相关推荐

  1. N天学习一个Linux命令之帮助命令:man

    前言 工作中每天都在使用常用的命令和非常用的命令,忘记了用法或者参数,都会bing一下,然后如此循环.一直没有真正的系统的深入的去了解命令的用法,我决定打破它.以前看到有人,每天学习一个linux命令 ...

  2. linux set命令例子,学习一个 Linux 命令:declare/typeset 命令

    原标题:学习一个 Linux 命令:declare/typeset 命令 1 功能简介 declare(别名 typeset)属 Shell 内建命令,用于申明 Shell 变量并设置变量属性,或查看 ...

  3. 学习一个 Linux 命令:pstree

    linux在工作中用的越来越多,接下来为大家介绍一下比较常用的命令pstree的使用方法 命令简介 pstree 命令以树状图的方式展现进程之间的派生关系. [root@centos7 ~]# pst ...

  4. 学习一个 Linux 命令:sort 命令

    今天来给大家介绍一下linux的sort命令 1.命令简介 以行为单位对文本文件的内容进行排序,将结果显示在标准输出,比较原则是从行首字符向后,依次按 ASCII 码值进行比较,最后按升序输出.如果 ...

  5. 学习一个 Linux 命令:shutdown 命令

    今天来给大家介绍一下linux的命令 shutdown 1.功能 shutdown 指令可以关闭所有程序,并依用户的需要,进行重启或关机操作. 使用 shutdown 命令时,在系统关机前,可以通知所 ...

  6. 学习一个 Linux 命令:realpath 命令

    转自:恋猫大鲤鱼链接:https://dablelv.blog.csdn.net/article/details/90710645 1.命令简介 realpath 用于获取指定目录或文件的绝对路径. ...

  7. N天学习一个Linux命令之top

    用途 查看机器负载以及进程资源占用情况,linux系统性能分析工具 用法 top -hv | -abcHimMsS -d delay -n iterations -p pid [, pid ...] ...

  8. N天学习一个linux命令之rsync

    用途 主要用于本地和远程主机同步文件 特性 1 使用增量传输算法(delta-transfer algorithm) 2 支持ssh,rsync协议 3 可以用于本地同步文件 4 本地和远程主机都需要 ...

  9. N天学习一个linux命令之lsof

    用途 列出进程已打开的文件,文件可以是常规文件,特殊文件,目录,socket,设备,共享库等.如果不带参数,lsof显示所有进程打开的所有文件. 用法 lsof [ -?abChlnNOPRtUvVX ...

  10. 每天一个linux命令目录

    开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每天一个linux命令.学习的主要参考资料为: 1.<鸟哥的linux私房菜> 2.http://codingstan ...

最新文章

  1. AndroidStudio自动补完包的快捷键
  2. Python第二十二天 stat模块 os.chmod方法 os.stat方法 pwd grp模块
  3. poj 2373(单调队列优化dp)
  4. 创建一个framework
  5. js(Dom+Bom)第八天
  6. 使用Jenkins来发布和代理.NetCore项目
  7. Bootstrap中实现图片圆角效果
  8. 10怎么设置pg接口_西门子S7300 PLC时间日期怎么读取?
  9. (08)Vivado时钟约束
  10. Unity UGUI优化:解决EventSystem耗时过长的问题 第一部分
  11. Java 学习如逆水行舟,不进则退,100 本 java 电子书推荐
  12. cad解除块的快捷命令_47个快捷键50个CAD技巧,快收藏起来
  13. matlab 电力系统分析 毕设,毕设开题报告_-基于matlab中电力系统模块集(psb的电力系统仿真研究.doc...
  14. opencms mysql_IDO分享 | 如何在centos下安装OpenCMS
  15. AS4120原厂单节 2A 开关型锂离子电池充电芯片
  16. 百度地图SDK for Android【Demo调起百度地图客户端导航功能】
  17. linux nmblookup 获取不到数据,接口中可以查到数据,为什么却获取不到呢?
  18. 整理了25个Python文本处理案例,收藏!
  19. 电子健康档案在国外的一些实践
  20. 雨听|Mac使用“打印”将文件保存为本地PDF

热门文章

  1. 什么是多态,多态的实现方法是什么?
  2. AOP:静态代理实现方式①通过继承②通过接口
  3. Spark集群新增节点方法
  4. 数据结构之 普利姆算法总结
  5. [MFC] 向文本编辑框写入数据与从其中读取数据
  6. 德州扑克的思考和实验
  7. 运维技术之二(2)、docker安装jumpserver
  8. 记一次vCenter转移CentOS6.5至workstation的网卡问题
  9. python脚本获取zabbix监控数据,并且通过邮件发送
  10. xmlhttp资料(收集)