Linux 日志查看 | grep 命令

Posted 挖坑埋你

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux 日志查看 | grep 命令相关的知识,希望对你有一定的参考价值。

概述


grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

语法


grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>][-d<进行动作>][-e<范本样式>][-f<范本文件>][--help][范本样式][文件或目录...]

参数


-a或--text 不要忽略二进制的数据。
-A<显示列数>或--after-context=<显示列数> 除了显示符合范本样式的那一列之外,并显示该列之后的内容。
-b或--byte-offset 在显示符合范本样式的那一列之前,标示出该列第一个字符的位编号。
-B<显示列数>或--before-context=<显示列数> 除了显示符合范本样式的那一列之外,并显示该列之前的内容。
-c或--count 计算符合范本样式的列数。
-C<显示列数>或--context=<显示列数>或-<显示列数> 除了显示符合范本样式的那一列之外,并显示该列之前后的内容。
-d<进行动作>或--directories=<进行动作> 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。
-e<范本样式>或--regexp=<范本样式> 指定字符串做为查找文件内容的范本样式。
-E或--extended-regexp 将范本样式为延伸的普通表示法来使用。
-f<范本文件>或--file=<范本文件> 指定范本文件,其内容含有一个或多个范本样式,让grep查找符合范本条件的文件内容,格式为每列一个范本样式。
-F或--fixed-regexp 将范本样式视为固定字符串的列表。
-G或--basic-regexp 将范本样式视为普通的表示法来使用。
-h或--no-filename 在显示符合范本样式的那一列之前,不标示该列所属的文件名称。
-H或--with-filename 在显示符合范本样式的那一列之前,表示该列所属的文件名称。
-i或--ignore-case 忽略字符大小写的差别。
-l或--file-with-matches 列出文件内容符合指定的范本样式的文件名称。
-L或--files-without-match 列出文件内容不符合指定的范本样式的文件名称。
-n或--line-number 在显示符合范本样式的那一列之前,标示出该列的列数编号。
-q或--quiet或--silent 不显示任何信息。
-r或--recursive 此参数的效果和指定"-d recurse"参数相同。
-s或--no-messages 不显示错误信息。
-v或--revert-match 反转查找。
-V或--version 显示版本信息。
-w或--word-regexp 只显示全字符合的列。
-x或--line-regexp 只显示全列符合的列。
-y 此参数的效果和指定"-i"参数相同。
--help 在线帮助。

grep命令常见用法


使用的日志文件内容查看:日志文件

在多个文件中搜索一个单词,命令会返回一个包含**“match_pattern”**的文本行

语法:

grep match_pattern file_1 file_2 file_3 ...
grep "match_pattern" file_1 file_2 file_3 ...

示例:

[root@peipei3514 usr]# grep AM test.log test2.log 
test.log:74 2018-05-14 13:52:13:712 SAMNR
test.log:183 2018-08-31 15:41:05:723 AMMBX
test2.log:282 2018-12-08 20:11:23:788 VAMDF
test2.log:292 2018-12-18 20:21:27:789 IMAMV
[root@peipei3514 usr]# grep "AM" test.log test2.log 
test.log:74 2018-05-14 13:52:13:712 SAMNR
test.log:183 2018-08-31 15:41:05:723 AMMBX
test2.log:282 2018-12-08 20:11:23:788 VAMDF
test2.log:292 2018-12-18 20:21:27:789 IMAMV
输出除**“match_pattern”**之外的所有行 -v 选项

语法:

grep -v "match_pattern" file_name

输出内容太多,就不粘贴了。

标记匹配颜色 --color=auto 选项

语法:

grep "match_pattern" file_name --color=auto

示例:

使用正则表达式 -E 选项

语法:

grep -E "[1-9]+"egrep "[1-9]+"

示例:搜索以数字9开头的行

[root@peipei3514 usr]# grep -E "^9" test.log 
9 2018-03-10 12:47:16:702 OEVDO
90 2018-05-30 14:08:19:713 EADRP
91 2018-05-31 14:09:10:714 ABJKY
92 2018-06-01 14:10:19:714 NRISH
93 2018-06-02 14:11:16:714 JGWFA
94 2018-06-03 14:12:09:714 KFIPP
95 2018-06-04 14:13:11:714 UXWRN
96 2018-06-05 14:14:10:714 BQREG
97 2018-06-06 14:15:20:714 XUOLW
98 2018-06-07 14:16:13:714 QMOGZ
99 2018-06-08 14:17:19:714 VDDZY
只输出文件中匹配到的部分 -o 选项

语法:

grep -o match_pattern file_name

示例:

[root@peipei3514 usr]# grep -o AM test.log
AM
AM
[root@peipei3514 usr]# echo this is a test line. | grep -o -E "[a-z]+\\."
line.
[root@peipei3514 usr]# echo this is a test line. | egrep -o "[a-z]+\\."
line.
统计文件或者文本中包含匹配字符串的行数 -c 选项

语法:

grep -c "text" file_name

示例:

[root@peipei3514 usr]# grep AM test.log
74 2018-05-14 13:52:13:712 SAMNR
183 2018-08-31 15:41:05:723 AMMBX
[root@peipei3514 usr]# grep -c AM test.log
2
输出包含匹配字符串的行数 -n 选项

语法:

grep "text" -n file_name
或
cat file_name | grep "text" -n

#多个文件
grep "text" -n file_1 file_2

示例:

[root@peipei3514 usr]# grep AM test.log
74 2018-05-14 13:52:13:712 SAMNR
183 2018-08-31 15:41:05:723 AMMBX
[root@peipei3514 usr]# grep -n AM test.log
77:74 2018-05-14 13:52:13:712 SAMNR
186:183 2018-08-31 15:41:05:723 AMMBX
打印样式匹配所位于的字符或字节偏移

语法:

grep "text" -b file_name

示例:

[root@peipei3514 usr]# grep -b AM test.log
2480:74 2018-05-14 13:52:13:712 SAMNR
6269:183 2018-08-31 15:41:05:723 AMMBX
[root@peipei3514 usr]# grep -b -o AM test.log
2508:AM
6297:AM
[root@peipei3514 usr]# echo gun is not unix | grep -b -o "not"
7:not

#一行中字符串的字符便宜是从该行的第一个字符开始计算,起始值为0。选项 -b -o 一般总是配合使用。
搜索多个文件并查找匹配文本在哪些文件中

语法:

grep -l "text" file1 file2 file3...

示例:

[root@peipei3514 usr]# grep -l AB test.log test2.log 
test.log
[root@peipei3514 usr]# grep -l AM test.log test2.log 
test.log
test2.log

grep递归搜索文件


改变一下目录结构:

[root@peipei3514 usr]# tree logs
logs
├── sublogs
│   ├── subsublogs
│   │   └── test5.log
│   ├── test3.log
│   └── test4.log
├── test2.log
└── test.log

2 directories, 5 files
在多级目录中对文本进行递归搜索

语法:

grep "text" . -r
# .表示当前目录。

示例:

忽略匹配样式中的字符大小写

语法:

grep "text" . -i

示例:

[root@peipei3514 logs]# grep "am" test.log
[root@peipei3514 logs]# grep "am" test.log -i
74 2018-05-14 13:52:13:712 SAMNR
183 2018-08-31 15:41:05:723 AMMBX
[root@peipei3514 logs]# grep "AM" test.log -i
74 2018-05-14 13:52:13:712 SAMNR
183 2018-08-31 15:41:05:723 AMMBX
选项 -e 指定多个匹配样式

语法:

grep -e "text" -e "text2" . -i

示例:

[root@peipei3514 logs]# grep -e "AM" -e "AB" test.log
38 2018-04-08 13:16:19:706 HABMP
74 2018-05-14 13:52:13:712 SAMNR
84 2018-05-24 14:02:04:713 ZABRH
91 2018-05-31 14:09:10:714 ABJKY
183 2018-08-31 15:41:05:723 AMMBX
[root@peipei3514 logs]# echo this is a text line | grep -e "is" -e "line" -o
is
is
line

#也可以使用-f选项来匹配多个样式,在样式文件中逐行写出需要匹配的字符。
[root@peipei3514 logs]# cat patfile
aaa
bbb
[root@peipei3514 logs]# echo aaa bbb ccc ddd eee | grep -f patfile -o
aaa
bbb
在grep搜索结果中包括或者排除指定文件

示例:

#只在目录中test3.log和test4.log文件中递归搜索字符"KS"
[root@peipei3514 logs]# grep "KS" . -r --include=test3,4.log
./sublogs/test3.log:426 2019-06-10 20:47:38:555 SSDKS
./sublogs/test4.log:736 2020-04-16 01:58:35:912 XMAKS
./sublogs/test4.log:743 2020-04-23 02:05:49:912 WBKSC
./sublogs/test4.log:783 2020-06-02 02:45:39:916 ASKST
#在搜索结果中排除所有README文件
grep "main()" . -r --exclude "README"

#实际测试时下面两个命令并没有管用
#在搜索结果中排除所有README文件
grep "main()" . -r --exclude "README"

#在搜索结果中排除filelist文件列表里的文件
grep "main()" . -r --exclude-from filelist
使用0值字节后缀的grep与xargs
#测试文件
[root@peipei3514 logs]# echo "aaa" > file1
[root@peipei3514 logs]# echo "bbb" > file2
[root@peipei3514 logs]# echo "aaa" > file3
[root@peipei3514 logs]# ll
总用量 32
-rw-r--r--. 1 root root    4 4月  10 14:22 file1
-rw-r--r--. 1 root root    4 4月  10 14:22 file2
-rw-r--r--. 1 root root    4 4月  10 14:22 file3
[root@peipei3514 logs]# grep "aaa" file* -lZ
file1file3[root@peipei3514 logs]# grep "aaa" file* -l
file1
file3
[root@peipei3514 logs]# grep "aaa" file* -lZ | xargs -0 rm
[root@peipei3514 logs]# ll
总用量 24
-rw-r--r--. 1 root root    4 4月  10 14:22 file2
-rw-r--r--. 1 root root    8 4月  10 13:55 patfile
drwxr-xr-x. 3 root root   58 4月  10 13:46 sublogs
-rw-r--r--. 1 root root 7000 4月  10 13:38 test2.log
-rw-r--r--. 1 root root 6899 4月  10 13:38 test.log

#执行后会删除file1和file3,grep输出用-Z选项来指定以0值字节作为终结符文件名(\\0),xargs -0 读取输入并用0值字节终结符分隔文件名,然后删除匹配文件,-Z通常和-l结合使用。
grep静默输出

语法:

grep -q "test" filename

#不会输出任何信息,如果命令运行成功返回0,失败则返回非0值。一般用于条件测试。
打印出匹配文本之前或者之后的行
[root@peipei3514 logs]# seq 10
1
2
3
4
5
6
7
8
9
10
#显示匹配某个结果之后的3行,使用 -A 选项:
[root@peipei3514 logs]# seq 10 | grep "5" -A 3
5
6
7
8

#显示匹配某个结果之前的3行,使用 -B 选项:
[root@peipei3514 logs]# seq 10 | grep "5" -B 3
2
3
4
5

#显示匹配某个结果的前三行和后三行,使用 -C 选项:
[root@peipei3514 logs]# seq 10 | grep "5" -C 3
2
3
4
5
6
7
8

#如果匹配结果有多个,会用“--”作为各匹配结果之间的分隔符:
[root@peipei3514 logs]# echo -e "a\\nb\\nc\\na\\nb\\nc"
a
b
c
a
b
c
[root@peipei3514 logs]# echo -e "a\\nb\\nc\\na\\nb\\nc" | grep a -A 1
a
b
--
a
b

以上是关于Linux 日志查看 | grep 命令的主要内容,如果未能解决你的问题,请参考以下文章

Linux grep 命令

每天学一个 Linux 命令:grep

每日一点Linux之Linux命令 -- grep

Linux查找字符串命令grep(转)

LINUX 使用grep命令查看某个指定时间段的日志

grep-查看日志