天天看點

linux grep一.指令格式:二.指令功能:三.指令參數:四. 規則表達式五. 使用執行個體

閱讀目錄(Content)

Linux系統中grep指令是一種強大的文本搜尋工具,它能使用正規表達式搜尋文本,并把匹 配的行列印出來。grep全稱是Global Regular Expression Print,表示全局正規表達式版本,它的使用權限是所有使用者。

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

grep可用于shell腳本,因為grep通過傳回一個狀态值來說明搜尋的狀态,如果模闆搜尋成功,則傳回0,如果搜尋不成功,則傳回1,如果搜尋的檔案不存在,則傳回2。我們利用這些傳回值就可進行一些自動化的文本處理工作。

回到頂部(go to top)

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"參數相同。

表達式
^ 錨定行的開始 如:'^grep'比對所有以grep開頭的行。
$ 錨定行的結束 如:'grep$'比對所有以grep結尾的行。
. 比對一個非換行符的字元 如:'gr.p'比對gr後接一個任意字元,然後是p。
* 比對零個或多個先前字元 如:'*grep'比對所有一個或多個空格後緊跟grep的行。
.* 一起用代表任意字元。
[] 比對一個指定範圍内的字元,如'[Gg]rep'比對Grep和grep。
[^] 比對一個不在指定範圍内的字元,如:'[^A-FH-Z]rep'比對不包含A-F和H-Z的一個字母開頭,緊跟rep的行。
(..) #标記比對字元,如'(love)',love被标記為1。
< 錨定單詞的開始,如:'<grep'比對包含以grep開頭的單詞的行。
> 錨定單詞的結束,如'grep>'比對包含以grep結尾的單詞的行。
x{m} 重複字元x,m次,如:'0{5}'比對包含5個o的行。
x{m,} 重複字元x,至少m次,如:'o{5,}'比對至少有5個o的行。
x{m,n} 重複字元x,至少m次,不多于n次,如:'o{5,10}'比對5--10個o的行。
\w 比對文字和數字字元,也就是[A-Za-z0-9],如:'G\w*p'比對以G後跟零個或多個文字或數字字元,然後是p。
\W \w的反置形式,比對一個或多個非單詞字元,如點号句号等。
\b 單詞鎖定符,如: '\bgrep\b'隻比對grep。

為了在不同國家的字元編碼中保持一至,POSIX(The Portable Operating System Interface)增加了特殊的字元類,如[:alnum:]是[A-Za-z0-9]的另一個寫法。要把它們放到[]号内才能成為正規表達式,如[A- Za-z0-9]或[[:alnum:]]。在linux下的grep除fgrep外,都支援POSIX的字元類。

字元
[:alnum:] 文字數字字元
[:alpha:] 文字字元
[:digit:] 數字字元
[:graph:] 非空字元(非空格、控制字元)
[:lower:] 小寫字元
[:cntrl:] 控制字元
[:print:] 非空字元(包括空格)
[:punct:] 标點符号
[:space:] 所有空白字元(新行,空格,制表符)
[:upper:] 大寫字元
[:xdigit:] 十六進制數字(0-9,a-f,A-F)

指令:

ps -ef|grep uwsgi
           

輸出:

[root@localhost autoAweme]# ps -ef|grep uwsgi
root      30568    795  0 12月01 ?      00:00:23 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      30578  30568  0 12月01 ?      00:00:00 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      59379    795  1 12月04 ?      00:13:13 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
root      59406  59379  0 12月04 ?      00:00:03 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
root     112483  86053  0 09:29 pts/1    00:00:00 grep --color=auto uwsgi

           

說明:

除最後一條記錄外,其他的都是查找出的程序;最後一條記錄結果是grep程序本身,并非真正要找的程序。

ps aux | grep uwsgi | grep -v "grep"
ps aux|grep [u]wsgi 
ps aux|grep /[u]wsgi 
           
[root@localhost test]# ps -aux | grep uwsgi 
root      30568  0.0  1.3 310244 53696 ?        S    12月01   0:23 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      30578  0.0  1.2 310244 49556 ?        S    12月01   0:00 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      59379  1.5  1.3 457952 50236 ?        Sl   12月04  13:50 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
root      59406  0.0  1.3 467760 53104 ?        S    12月04   0:03 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
root      87765  0.0  0.0 112704  1016 pts/1    R+   10:14   0:00 grep --color=auto uwsgi
[root@localhost test]# ps -aux | grep uwsgi | grep -v "grep"
root      30568  0.0  1.3 310244 53696 ?        S    12月01   0:23 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      30578  0.0  1.2 310244 49556 ?        S    12月01   0:00 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      59379  1.5  1.3 457952 50236 ?        Sl   12月04  13:50 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
root      59406  0.0  1.3 467760 53104 ?        S    12月04   0:03 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini

[root@localhost test]# ps -aux | grep [u]wsgi
root      30568  0.0  1.3 310244 53696 ?        S    12月01   0:23 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      30578  0.0  1.2 310244 49556 ?        S    12月01   0:00 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      59379  1.5  1.3 457952 50236 ?        Sl   12月04  13:45 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
root      59406  0.0  1.3 467760 53104 ?        S    12月04   0:03 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
[root@localhost test]# ps -aux | grep \[u]wsgi
root      30568  0.0  1.3 310244 53696 ?        S    12月01   0:23 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      30578  0.0  1.2 310244 49556 ?        S    12月01   0:00 /home/hc/project/envs/pgc/bin/uwsgi --ini /home/hc/project/pgc.ini
root      59379  1.5  1.3 457952 50236 ?        Sl   12月04  13:45 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini
root      59406  0.0  1.3 467760 53104 ?        S    12月04   0:03 /home/hc/project/envs/autoAweme/bin/uwsgi --ini /home/hc/project/autoAweme.ini

           

ps -ef|grep uwsgi -c
ps -ef|grep -c uwsgi
           
[root@localhost autoAweme]# ps -ef|grep uwsgi -c
5
[root@localhost autoAweme]# ps -ef|grep -c uwsgi 
5
           

cat 3.log | grep -f 4.log 
           
[root@localhost test]# cat 3.log 
1
2
3
[root@localhost test]# cat 4.log 
1
12
5
43
[root@localhost test]# cat 3.log | grep -f 4.log 
1
[root@localhost test]# cat 4.log | grep -f 3.log 
1
12
43

           

cat 3.log | grep -f 4.log 從3.log檔案中比對出含有4.log中關鍵字的行并輸出

cat 4.log | grep -f 3.log 從4.log檔案中比對出含有3.log中關鍵字的行并輸出

如:4.log中的關鍵字有1,12,5,43四個,在3.log中無論是完全比對還是部分比對隻能比對到1,并輸出

在 3.log中關鍵字為1,2,3, 是以在4.log中比對3時,能完全比對到含有1,2,3的行,并把比對部分着色表示輸出

cat 4.log | grep -nf 3.log
           
[root@localhost test]# cat 4.log | grep -nf 3.log 
1:1
2:12
4:43
[root@localhost test]# cat 3.log | grep -nf 4.log 
1:1

           

輸出4.log檔案中含有從3.log 檔案中讀取出的關鍵詞的内容行,并顯示每一行的行号,冒号(:)左邊是行号,右邊是比對的内容

grep "1" 4.log 
           
[root@localhost test]# cat 4.log 
1
12
"1"
5
43
[root@localhost test]# grep  1  4.log 
1
12
"1"
[root@localhost test]# grep  '1'  4.log 
1
12
"1"
[root@localhost test]# grep  "1"  4.log 
1
12
"1"
[root@localhost test]# grep  \"1\" 4.log 
"1"

           

有無引号,或者單雙引号 效果是一樣的,但是加上引号可讀性好一點。另外如果要查詢帶引号的内容,需要用\進行轉義

grep '1' 3.log 4.log 
           
[root@localhost test]# grep '1' 3.log 4.log 
3.log:1
4.log:1
4.log:12
4.log:"1"
[root@localhost test]# grep -n 1 3.log 4.log 
3.log:1:1
4.log:1:1
4.log:2:12
4.log:3:"1"

           

多檔案時,輸出查詢到的資訊内容行時,會把檔案的命名放在在行的最左邊輸出并且加上":"作為标示符分隔,如果用了-n展示行号,則第二個:的左邊是行号,最右邊的是比對内容

cat 4.log |grep ^1
           
[root@localhost test]# cat 4.log 
1
12
"1"
5
43
[root@localhost test]# cat 4.log |grep ^1
1
12

           

cat 4.log |grep ^[^1]
           
[root@localhost test]# cat 4.log |grep ^[^1]
"1"
5
43

           

cat 4.log |grep 3$
           
[root@localhost test]# cat 4.log |grep 3$
43

           

grep 1 *log
           
[root@localhost test]# ls
1.log  2.log  2.log.back  3.log  4.log
[root@localhost test]# grep 1 *log
3.log:1
4.log:1
4.log:12
4.log:"1"

           

grep -r 僅此一條 /home/hc
           
[root@localhost hc]# grep -r 僅此一條 /home/hc
/home/hc/test/2.log.back:僅此一條,我是第五行
[root@localhost hc]# 

           

查找指定目錄/home/hc 及其子目錄(如果存在子目錄的話)下所有檔案中包含字元串"僅此一條"的檔案,并列印出該字元串所在行的内容

grep -v 僅此一條 *
           
[root@localhost test]# ls
1.log  2.log  2.log.back  3.log  4.log
[root@localhost test]# grep 僅此一條 *
2.log.back:僅此一條,我是第五行
[root@localhost test]# grep -v 僅此一條 *
1.log:hnlinux
1.log:
1.log:hpython.cn
1.log:
1.log:ubuntu
1.log:
1.log:ubuntu linux
1.log:
1.log:redhat
1.log:
1.log:Redhat
1.log:
1.log:linuxmint
2.log:linux
2.log:
2.log:Redhat
2.log.back:第一行
2.log.back:第二行
2.log.back:我是log2第3行
2.log.back:第四行
3.log:1
3.log:2
3.log:3
4.log:1
4.log:12
4.log:"1"
4.log:5
4.log:43

           

查找目前目錄下所有檔案,找出其中不包含"僅此一條"的行

繼續閱讀