1. 程式人生 > 實用技巧 >每日一個Linux 命令-Grep

每日一個Linux 命令-Grep

Grep

grep (global search regular expression(RE) and print out the line,全面搜尋正則表示式並把行打印出來)是一種強大的文字搜尋工具,它能使用正則表示式搜尋文字,並把匹配的行打印出來。Unix的grep家族包括grep、egrep和fgrep。egrep和fgrep的命令只跟grep有很小不同。egrep是grep的擴充套件,支援更多的re元字元, fgrep就是fixed grep或fast grep,它們把所有的字母都看作單詞,也就是說,正則表示式中的元字元表示回其自身的字面意義,不再特殊。Linux使用GNU版本的grep。它功能更強,可以通過-G、-E、-F命令列選項來使用egrep和fgrep的功能。

grep的工作方式是這樣的,它在一個或多個檔案中搜索字串模板。如果模板包括空格,則必須被引用,模板後的所有字串被看作檔名。搜尋的結果被送到螢幕,不影響原檔案內容。

grep可用於shell指令碼,因為grep通過返回一個狀態值來說明搜尋的狀態,如果模板搜尋成功,則返回0,如果搜尋不成功,則返回1,如果搜尋的檔案不存在,則返回2。我們利用這些返回值就可進行一些自動化的文字處理工作。

語法

grep [-abcEFGhHilLnqrsvVwxy][-A<顯示列數>][-B<顯示列數>][-C<顯示列數>][-d<進行動作>][-e<範本樣式>][-f<範本檔案>][--help][範本樣式][檔案或目錄...]

匹配模式選擇:
 -E, --extended-regexp     擴充套件正則表示式egrep
 -F, --fixed-strings       一個換行符分隔的字串的集合fgrep
 -G, --basic-regexp        基本正則
 -P, --perl-regexp         呼叫的perl正則
 -e, --regexp=PATTERN      後面根正則模式,預設無
 -f, --file=FILE           從檔案中獲得匹配模式
 -i, --ignore-case         不區分大小寫
 -w, --word-regexp         匹配整個單詞
 
-x, --line-regexp 匹配整行 -z, --null-data 一個 0 位元組的資料行,但不是空行 雜項: -s, --no-messages 不顯示錯誤資訊 -v, --invert-match 顯示不匹配的行 -V, --version 顯示版本號 --help 顯示幫助資訊 --mmap use memory-mapped input if possible 輸入控制: -m, --max-count=NUM 匹配的最大數 -b, --byte-offset 列印匹配行前面列印該行所在的塊號碼。 -n, --line-number 顯示的加上匹配所在的行號 --line-buffered 重新整理輸出每一行 -H, --with-filename 當搜尋多個檔案時,顯示匹配檔名字首 -h, --no-filename 當搜尋多個檔案時,不顯示匹配檔名字首 --label=LABEL print LABEL as filename for standard input -o, --only-matching 只顯示一行中匹配PATTERN 的部分 -q, --quiet, --silent 不顯示任何東西 --binary-files=TYPE 假定二進位制檔案的TYPE 型別; TYPE 可以是`binary', `text', 或`without-match' -a, --text 匹配二進位制的東西 -I 不匹配二進位制的東西 -d, --directories=ACTION 目錄操作,讀取,遞迴,跳過 -D, --devices=ACTION 設定對裝置,FIFO,管道的操作,讀取,跳過 -R, -r, --recursive 遞迴呼叫 --include=PATTERN 只查詢匹配FILE_PATTERN 的檔案 --exclude=PATTERN 跳過匹配FILE_PATTERN 的檔案和目錄 --exclude-from=FILE 跳過所有除FILE 以外的檔案 -L, --files-without-match 匹配多個檔案時,顯示不匹配的檔名 -l, --files-with-matches 匹配多個檔案時,顯示匹配的檔名 -c, --count 顯示匹配的行數 -Z, --null 在FILE 檔案最後列印空字元 檔案控制: -B, --before-context=NUM 列印匹配本身以及前面的幾個行由NUM控制 -A, --after-context=NUM 列印匹配本身以及隨後的幾個行由NUM控制 -C, --context=NUM 列印匹配本身以及隨後,前面的幾個行由NUM控制 -NUM 根-C的用法一樣的 --color[=WHEN], --colour[=WHEN] 使用標誌高亮匹配字串; -U, --binary 使用標誌高亮匹配字串; -u, --unix-byte-offsets 當CR 字元不存在,報告位元組偏移(MSDOS 模式)
引數

例項

1、在當前目錄中,查詢字尾有 file 字樣的檔案中包含 test 字串的檔案,並打印出該字串的行。此時,可以使用如下命令:

[root@localhost test]# grep test test* #查詢字首有“test”的檔案包含“test”字串的檔案 
testfile1:This a Linux testfile
! #列出testfile1 檔案中包含test字元的行 testfile_2:This is a linux testfile! #列出testfile_2 檔案中包含test字元的行 testfile_2:Linux test #列出testfile_2 檔案中包含test字元的行

2、以遞迴的方式查詢符合條件的檔案。例如,查詢指定目錄/etc/acpi 及其子目錄(如果存在子目錄的話)下所有檔案中包含字串"update"的檔案,並打印出該字串所在行的內容,使用的命令為:

[root@localhost test]# grep -r update /etc/acpi #以遞迴的方式查詢“etc/acpi”  

#下包含“update”的檔案
/etc/acpi/ac.d/85-anacron.sh:# (Things like the slocate updatedb cause a lot of IO.) Rather than /etc/acpi/resume.d/85-anacron.sh:# (Things like the slocate updatedb cause a lot of IO.) Rather than /etc/acpi/events/thinkpad-cmos:action=/usr/sbin/thinkpad-keys--update

3、反向查詢。前面各個例子是查詢並打印出符合條件的行,通過"-v"引數可以打印出不符合條件行的內容。查詢檔名中包含 test 的檔案中不包含test 的行,此時,使用的命令為:

[root@localhost test]# grep -v test* #查詢檔名中包含test 的檔案中不包含test 的行  

testfile1:helLinux
! testfile1:Linis a free Unix-type operating system. testfile1:Lin testfile_1:HELLO LINUX! testfile_1:LINUX IS A FREE UNIX-TYPE OPTERATING SYSTEM. testfile_1:THIS IS A LINUX TESTFILE! testfile_2:HELLO LINUX! testfile_2:Linux is a free unix-type opterating system.

4、匹配以HELLO開頭或者以test開頭的行,注意反斜槓,根上面一個例子一樣,-e預設是省去的

[root@localhost test]# cat test |grep -e '^\(HELLO\|test\)'  

HELLO LINUX!
test Linux !

5、匹配以sui開頭,只含有字母

[root@localhost test]# echo 'suixinnan' |grep '^sui[a-z]*$'  

suixinnan

新建測試檔案

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa
DADddd:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
suixinnan:x:1000:100:,,,:/home/suixinnan:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
policykit:x:102:1005:Po

a,匹配以bin開頭的行,用的egrep,在這裡可以換成-F,-G

[root@localhost test]# cat test |grep -E '^bin'  

bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa  

b,在匹配的行前面加上該行在檔案中,或者輸出中所在的行號

[root@localhost test]# cat test|grep -n suixin

7:suixinnan:x:1000:100:,,,:/home/suixinnan:/bin/bash  

c,不匹配以bin開頭的行,並顯示行號

[root@localhost test]# cat test|grep -nv '^bin'  

root:x:0:0:root:/root:/bin/bash
DADddd:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
suixinnan:x:1000:100:,,,:/home/suixinnan:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
policykit:x:102:1005:Po

d,顯示匹配的個數,不顯示內容

[root@localhost test]#  cat test|grep -c sui  

1  

e,匹配system,沒有加-i沒有匹配到東西。

[root@localhost test]# grep  system test  
[root@localhost test]# grep -ni  system test  

9:dbus:x:81:81:System message bus:/:/bin/false  

f,匹配sui沒有匹配到東西,匹配suixinnan能匹配到,因為在test檔案中,有suixinnan這個單詞

[root@localhost test]#  cat test|grep -w sui  
[root@localhost test]# cat test|grep -w suixinnan 

suixinnan:x:1000:100:,,,:/home/suixinnan:/bin/bash  

g,在這裡-x後面東西,和輸出中的整行相同時,才會輸出

[root@localhost test]# echo "aaaaaa" |grep -x aaa  
[root@localhost test]# echo "aaaa" |grep -x aaaa  

aaaa  

h,最多隻匹配一次,如果把-m 1去掉的話,會有多個(前提test 檔案中有多個)

[root@localhost test]# cat test |grep -m 1 suixin 
 
suixinnan:x:1000:100:,,,:/home/suixinnan:/bin/bash  

i,匹配行的前面顯示塊號,這個塊號是幹什麼的,不知道,有誰知道可否告訴我一下

[root@localhost test]# cat test |grep -b suixin
 
241:suixinnan:x:1000:100:,,,:/home/suixinnan:/bin/bash  


j,多檔案匹配時,在匹配的行前面加上檔名

[root@localhost test]# grep -H 'root' test test2 testbak  

test:root:x:0:0:root:/root:/bin/bash  
test2:root  
testbak:root:x:0:0:root:/root:/bin/bash 

k,多檔案匹配時,在匹配的行前面不加上檔名

[root@localhost test]# grep -h 'root' test test2 testbak  

root:x:0:0:root:/root:/bin/bash  
root  
root:x:0:0:root:/root:/bin/bash  

l,多檔案匹配時,顯示匹配檔案的檔名

[root@localhost test]# grep -l 'root' test test2 testbak DAta  

test  
test2  
testbak  

m,沒有-o時,有一行匹配,這一行裡面有3個root,加上-o後,這個3個root就出來了

[root@localhost test]# grep  'root' test 
 
root:x:0:0:root:/root:/bin/bash
  
[root@localhost test]# grep -o 'root' test 
 
root  
root  
root  

n,遞迴顯示匹配的內容,在test目錄下面建個mytest目錄,copy test目錄下面的test檔案到mytest下面,能看到上面的結果

[root@localhost test]# grep test -R /tmp/test/mytest
  
/tmp/test/mytest/test:test:x:1003:1003::/home/test:/bin/bash  
/tmp/test/mytest/test:@zhangying:*:1004:1004::/home/test:/bin/bash  

o,顯示匹配root後面的3行

[root@localhost test]# cat test |grep -A 3 root  
root:x:
0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa daemon:x:2:2:daemon:/sbin:/bin/false mail:x:8:12:mail:/var/spool/mail:/bin/false

場景:

系統報警顯示了時間,但是日誌檔案太大無法直接 cat 檢視。(查詢含有特定文字的檔案,並拿到這些文字所在的行)

解決:

[root@localhost test]#grep -n '2020-08-24 00:01:11' *.log

從根目錄開始查詢所有副檔名為 .log 的文字檔案,並找出包含 "ERROR" 的行:

[root@localhost test]# find / -type f -name "*.log" | xargs grep "ERROR"

從當前目錄開始查詢所有副檔名為 .in 的文字檔案,並找出包含 "thermcontact" 的行:

[root@localhost test]#find . -name "*.in" | xargs grep "thermcontact"
遞迴從所有檔案中查詢匹配的內容,檔名可不同
[root@localhost test]# grep -R C1079651000621  *   

20200727/503/20200701000104001317.xml:            C1079651000621
20200727/503/20200701000104001317.xml:            C1079651000621
20200727/503/20200701000104001333.xml:            C1079651000621
有一點要注意,您必需提供一個檔案過濾方式(搜尋全部檔案的話用 *)。如果您忘了,’grep’會一直等著,直到該程式被中斷。如果您遇到了這樣的情況,按 ,然後再試。
    下面還有一些有意思的命令列引數:
    grep -i pattern files :不區分大小寫地搜尋。預設情況區分大小寫,
    grep -l pattern files :只列出匹配的檔名,
    grep -L pattern files :列出不匹配的檔名,
    grep -w pattern files :只匹配整個單詞,而不是字串的一部分(如匹配’magic’,而不是’magical’),
    grep -C number pattern files :匹配的上下文分別顯示[number]行,
    grep pattern1 | pattern2 files :顯示匹配 pattern1 或 pattern2 的行,
    例如:grep "abc\|xyz" testfile 表示過濾包含abc或xyz的行
    grep pattern1 files | grep pattern2 :顯示既匹配 pattern1 又匹配 pattern2 的行。
    grep -n pattern files 即可顯示行號資訊
    grep -c pattern files 即可查詢總行數
    還有些用於搜尋的特殊符號:\< 和 \> 分別標註單詞的開始與結尾。
    例如:
    grep man * 會匹配 ‘Batman’、’manic’、’man’等,
    grep ‘\<man’ * 匹配’manic’和’man’,但不是’Batman’,
    grep ‘\<man\>’ 只匹配’man’,而不是’Batman’或’manic’等其他的字串。
    ‘^’: 指匹配的字串在行首,
    ‘$’: 指匹配的字串在行 尾,
    
    用grep查詢結構體 grep -R "struct task_struct {" /usr/src 加-n可以顯示行號PS1=$ 進入到家目錄在.bashrc    中