grep命令目前是查看日志使用最高频的一个命令,不过它的有些参数容易忘记,这里补几个示例,供日后查看。
参考:
https://www.gnu.org/software/grep/manual/grep.html
https://www.makeuseof.com/grep-command-practical-examples/
https://www.golinuxcloud.com/grep-command-in-linux/
https://geekflare.com/grep-command-examples/
https://www.tecmint.com/12-practical-examples-of-linux-grep-command/
上面的示例已经比较丰富了,我也没有太多新的花样,可以提供,为了加强些印象,我还是把自己实验的例子贴一下。
假设:现在所在的目录有如下文件a.log、a.log.2022-12-18、b.log、b.log.2022-12-18、usr/c.log、usr/c.log.2022-12-18
1:grep --help
查看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: [email protected] GNU Grep home page: <http://www.gnu.org/software/grep/> General help using GNU software: <http://www.gnu.org/gethelp/>View Code
2:grep error a.log
查看a.log文件中是否包含error字样的文本信息
3:grep error *.log
查看a.log、b.log文件中是否包含error字样的文本信息
4:grep error *.log.*
查看a.log.2022-12-18、b.log.2022-12-18文件中是否包含error字样的文本信息
5:grep error *
查看a.log、b.log、a.log.2022-12-18、b.log.2022-12-18文件中是否包含error字样的文本信息
6:grep -r error *
查看a.log、b.log、a.log.2022-12-18、b.log.2022-12-18、usr/c.log、usr/c.log.2022-12-18文件中是否包含error字样的文本信息,递归查询,这个在有子文件夹的情况查询比较有用
7:grep -w proc_time=100 a.log
查看a.log文件中是否包含 proc_time=100 完整字样的文本信息,注意:此时匹配的是 proc_time=100 当做一个完整的单词
-w, --word-regexp force PATTERN to match only whole words
8:grep -F proc_time=100 a.log
查看a.log文件中是否包含 proc_time=100 固定字样的文本信息,注意:此时匹配的是 proc_time=100 当做一个固定的文本信息
-F, --fixed-strings PATTERN is a set of newline-separated fixed strings
7和8有一个细微的差异,假设:a.log中有 proc_time=100 ,则 grep -F proc_time= a.log 能匹配到信息,而 grep -w proc_time= a.log 却匹配不到信息
9:grep -i proc_time=100 a.log
查看a.log文件中是否包含 proc_time=100 字样的文本信息,注意:此次忽略字母的大小写敏感性
-i, --ignore-case ignore case distinctions
10:grep -c proc_time=100 a.log
查看a.log文件中包含 proc_time=100 字样的文本信息出现的次数,注意:此次忽略字母的大小写敏感性
-c, --count print only a count of matching lines per FILE
11:grep -A 10 error a.log
查看a.log文件中包含 error 字样的文本信息,且打印出匹配内容后的10行内容,注意:这种我常常用来查看错误日志的堆栈信息
-A, --after-context=NUM print NUM lines of trailing context
12:grep -B 10 error a.log
查看a.log文件中包含 error 字样的文本信息,且打印出匹配内容前的10行内容
-B, --before-context=NUM print NUM lines of leading context
13:grep -C 10 error a.log
查看a.log文件中包含 error 字样的文本信息,且打印出匹配内容后的10行内容,注意:这个输出和 -B参数类似
-C, --context=NUM print NUM lines of output context
14:grep -n error a.log
查看a.log文件中包含 error 字样的文本信息,且打印出输出内容的行号
-n, --line-number print line number with output lines
15:grep -v error a.log
查看a.log文件中不包含 error 字样的文本信息,注意:这里是反向搜索,有时候比较有用
-v, --invert-match select non-matching lines
16:grep -m 10 error a.log
查看a.log文件中包含 error 字样的文本信息,注意:只输出匹配到的行数,比如,这里是10行,注意这里是10是最大的匹配行数,也就是最多输出10行,防止输入的内容过多
-m, --max-count=NUM stop after NUM matches
17:grep -l error *.log
查看那些文件中包含 error 字样的文本信息,注意:这里只输出匹配的文件的名字
-l, --files-with-matches print only names of FILEs containing matches
18:grep -e error -e errno -e errmsg *.log
查看那些文件中包含 error + errno + errmsg 字样的文本信息,注意:输出的内容,不是最精确的,会把第一个模式匹配到的内容也打印出来
-e, --regexp=PATTERN use PATTERN for matching
19:grep error errmsg *.log | grep errno | grep errmsg
查看那些文件中包含 error + errno + errmsg 字样的文本信息,注意:输出的内容,是精确的,
20:grep结合正则表达式,这种我实际工作中用的较少,实际工作用,使用的最多的就是根据某个关键字或多个关键字来查找相关的日志信息
标签:grep,log,示例,--,PATTERN,命令,NUM,error From: https://www.cnblogs.com/godtrue/p/16990745.html