首页 > 代码库 > N天学习一个Linux命令之帮助命令:grep

N天学习一个Linux命令之帮助命令:grep

前言
任何系统都会出问题,出了问题一般怎么排查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

 

N天学习一个Linux命令之帮助命令:grep