1. 程式人生 > >Linux日誌檢視(9)— grep 命令(詳解+例項)

Linux日誌檢視(9)— 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=test{3,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 410 14:22 file1
-rw-r--r--. 1 root root    4 410 14:22 file2
-rw-r--r--. 1 root root    4 410 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 410 14:22 file2
-rw-r--r--. 1 root root    8 410 13:55 patfile
drwxr-xr-x. 3 root root   58 410 13:46 sublogs
-rw-r--r--. 1 root root 7000 410 13:38 test2.log
-rw-r--r--. 1 root root 6899 410 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