咆哮中寫出Linux Shell經常使用技巧(二)

 

grep家族:linux

1. grep退出狀態:正則表達式

0: 表示成功;sass

1: 表示在所提供的文件沒法找到匹配的pattern;bash

2: 表示參數中提供的文件不存在。命令行

見以下示例:blog

/> grep 'root' /etc/passwd遞歸

root:x:0:0:root:/root:/bin/bashci

operator:x:11:0:operator:/root:/sbin/nologin字符串

/> echo $?get

0

/> grep 'root1' /etc/passwd #用戶root1並不存在

/> echo $?

1

/> grep 'root' /etc/passwd1 #這裏的/etc/passwd1文件並不存在

grep: /etc/passwd1: No such file or directory

/> echo $?

2

2. grep中應用正則表達式的實例:

須要說明的是下面所涉及的正則表達式在上一篇中已經給出了詳細的說明,所以在看下面例子的時候,能夠與前一篇的正則說明部分結合着看。

/> cat testfile

northwest NW Charles Main 3.0 .98 3 34

western WE Sharon Gray 5.3 .97 5 23

southwest SW Lewis Dalsass 2.7 .8 2 18

southern SO Suan Chin 5.1 .95 4 15

southeast SE Patricia Hemenway 4.0 .7 4 17

eastern EA TB Savage 4.4 .84 5 20

northeast NE AM Main Jr. 5.1 .94 3 13

north NO Margot Weber 4.5 .89 5 9

central CT Ann Stephens 5.7 .94 5 13

/> grep NW testfile #打印出testfile中全部包含NW的行。

northwest NW Charles Main 3.0 .98 3 34

/> grep '^n' testfile #打印出以n開頭的行。

northwest NW Charles Main 3.0 .98 3 34

northeast NE AM Main Jr. 5.1 .94 3 13

north NO Margot Weber 4.5 .89 5 9

/> grep '4$' testfile #打印出以4結尾的行。

northwest NW Charles Main 3.0 .98 3 34

/> grep '5\..' testfile #打印出第一個字符是5,後面跟着一個.字符,在後面是任意字符的行。

western WE Sharon Gray 5.3 .97 5 23

southern SO Suan Chin 5.1 .95 4 15

northeast NE AM Main Jr. 5.1 .94 3 13

central CT Ann Stephens 5.7 .94 5 13

/> grep '\.5' testfile #打印出全部包含.5的行。

north NO Margot Weber 4.5 .89 5 9

/> grep '^[we]' testfile #打印出全部以w或e開頭的行。

western WE Sharon Gray 5.3 .97 5 23

eastern EA TB Savage 4.4 .84 5 20

/> grep '[^0-9]' testfile #打印出全部不是以0-9開頭的行。

northwest NW Charles Main 3.0 .98 3 34

western WE Sharon Gray 5.3 .97 5 23

southwest SW Lewis Dalsass 2.7 .8 2 18

southern SO Suan Chin 5.1 .95 4 15

southeast SE Patricia Hemenway 4.0 .7 4 17

eastern EA TB Savage 4.4 .84 5 20

northeast NE AM Main Jr. 5.1 .94 3 13

north NO Margot Weber 4.5 .89 5 9

central CT Ann Stephens 5.7 .94 5 13

/> grep '[A-Z][A-Z] [A-Z]' testfile #打印出全部包含前兩個字符是大寫字符,後面緊跟一個空格及一個大寫字母的行。

eastern EA TB Savage 4.4 .84 5 20

northeast NE AM Main Jr. 5.1 .94 3 13

注:在執行以上命令時,若是不能獲得預期的結果,即grep忽略了大小寫,致使這一問題的緣由極可能是當前環境的本地化的設置問題。對於以上命令,若是我將當前語言設置爲en_US的時候,它會打印出全部的行,當我將其修改成中文環境時,就能獲得我如今的輸出了。

/> export LANG=zh_CN #設置當前的語言環境爲中文。

/> export LANG=en_US #設置當前的語言環境爲美國。

/> export LANG=en_Br #設置當前的語言環境爲英國。

/> grep '[a-z]\{9\}' testfile #打印全部包含每一個字符串至少有9個連續小寫字符的字符串的行。

northwest NW Charles Main 3.0 .98 3 34

southwest SW Lewis Dalsass 2.7 .8 2 18

southeast SE Patricia Hemenway 4.0 .7 4 17

northeast NE AM Main Jr. 5.1 .94 3 13

#第一個字符是3,緊跟着一個句點,而後是任意一個數字,而後是任意個任意字符,而後又是一個3,而後是製表符,而後又是一個3,須要說明的是,下面正則中的\1表示\(3\)。

/> grep '\(3\)\.[0-9].*\1 *\1' testfile

northwest NW Charles Main 3.0 .98 3 34

/> grep '\<north' testfile #打印全部以north開頭的單詞的行。

northwest NW Charles Main 3.0 .98 3 34

northeast NE AM Main Jr. 5.1 .94 3 13

north NO Margot Weber 4.5 .89 5 9

/> grep '\<north\>' testfile #打印全部包含單詞north的行。

north NO Margot Weber 4.5 .89 5 9

/> grep '^n\w*' testfile #第一個字符是n,後面是任意字母或者數字。

northwest NW Charles Main 3.0 .98 3 34

northeast NE AM Main Jr. 5.1 .94 3 13

north NO Margot Weber 4.5 .89 5 9

3. 擴展grep(grep -E 或者 egrep):

使用擴展grep的主要好處是增長了額外的正則表達式元字符集。下面咱們仍是繼續使用實例來演示擴展grep。

/> egrep 'NW|EA' testfile #打印全部包含NW或EA的行。若是不是使用egrep,而是grep,將不會有結果查出。

northwest NW Charles Main 3.0 .98 3 34

eastern EA TB Savage 4.4 .84 5 20

/> grep 'NW\|EA' testfile #對於標準grep,若是在擴展元字符前面加\,grep會自動啓用擴展選項-E。

northwest NW Charles Main 3.0 .98 3 34

eastern EA TB Savage 4.4 .84 5 20

/> egrep '3+' testfile

/> grep -E '3+' testfile

/> grep '3\+' testfile #這3條命令將會打印出相同的結果,即全部包含一個或多個3的行。

northwest NW Charles Main 3.0 .98 3 34

western WE Sharon Gray 5.3 .97 5 23

northeast NE AM Main Jr. 5.1 .94 3 13

central CT Ann Stephens 5.7 .94 5 13

/> egrep '2\.?[0-9]' testfile

/> grep -E '2\.?[0-9]' testfile

/> grep '2\.\?[0-9]' testfile #首先含有2字符,其後緊跟着0個或1個點,後面再是0和9之間的數字。

western WE Sharon Gray 5.3 .97 5 23

southwest SW Lewis Dalsass 2.7 .8 2 18

eastern EA TB Savage 4.4 .84 5 20

/> egrep '(no)+' testfile

/> grep -E '(no)+' testfile

/> grep '\(no\)\+' testfile #3個命令返回相同結果,即打印一個或者多個連續的no的行。

northwest NW Charles Main 3.0 .98 3 34

northeast NE AM Main Jr. 5.1 .94 3 13

north NO Margot Weber 4.5 .89 5 9

/> grep -E '\w+\W+[ABC]' testfile #首先是一個或者多個字母,緊跟着一個或者多個非字母數字,最後一個是ABC中的一個。

northwest NW Charles Main 3.0 .98 3 34

southern SO Suan Chin 5.1 .95 4 15

northeast NE AM Main Jr. 5.1 .94 3 13

central CT Ann Stephens 5.7 .94 5 13

/> egrep '[Ss](h|u)' testfile

/> grep -E '[Ss](h|u)' testfile

/> grep '[Ss]\(h\|u\)' testfile #3個命令返回相同結果,即以S或s開頭,緊跟着h或者u的行。

western WE Sharon Gray 5.3 .97 5 23

southern SO Suan Chin 5.1 .95 4 15

/> egrep 'w(es)t.*\1' testfile #west開頭,其中es爲\1的值,後面緊跟着任意數量的任意字符,最後還有一個es出如今該行。

northwest NW Charles Main 3.0 .98 3 34

4. grep選項:

這裏先列出grep經常使用的命令行選項:

選項說明-c只顯示有多少行匹配,而不具體顯示匹配的行。-h不顯示文件名。-i在字符串比較的時候忽略大小寫。-l只顯示包含匹配模板的行的文件名清單。-L只顯示不包含匹配模板的行的文件名清單。-n在每一行前面打印改行在文件中的行數。-v反向檢索,只顯示不匹配的行。-w只顯示完整單詞的匹配。-x只顯示完整行的匹配。-r/-R若是文件參數是目錄,該選項將遞歸搜索該目錄下的全部子目錄和文件。

/> grep -n '^south' testfile #-n選項在每個匹配行的前面打印行號。

3:southwest SW Lewis Dalsass 2.7 .8 2 18

4:southern SO Suan Chin 5.1 .95 4 15

5:southeast SE Patricia Hemenway 4.0 .7 4 17

/> grep -i 'pat' testfile #-i選項關閉了大小寫敏感。

southeast SE Patricia Hemenway 4.0 .7 4 17

/> grep -v 'Suan Chin' testfile #打印全部不包含Suan Chin的行。

northwest NW Charles Main 3.0 .98 3 34

western WE Sharon Gray 5.3 .97 5 23

southwest SW Lewis Dalsass 2.7 .8 2 18

southeast SE Patricia Hemenway 4.0 .7 4 17

eastern EA TB Savage 4.4 .84 5 20

northeast NE AM Main Jr. 5.1 .94 3 13

north NO Margot Weber 4.5 .89 5 9

central CT Ann Stephens 5.7 .94 5 13

/> grep -l 'ss' testfile #-l使得grep只打印匹配的文件名,而不打印匹配的行。

testfile

/> grep -c 'west' testfile #-c使得grep只打印有多少匹配模板的行。

3

/> grep -w 'north' testfile #-w只打印整個單詞匹配的行。

north NO Margot Weber 4.5 .89 5 9

/> grep -C 2 Patricia testfile #打印匹配行及其上下各兩行。

southwest SW Lewis Dalsass 2.7 .8 2 18

southern SO Suan Chin 5.1 .95 4 15

southeast SE Patricia Hemenway 4.0 .7 4 17

eastern EA TB Savage 4.4 .84 5 20

northeast NE AM Main Jr. 5.1 .94 3 13

/> grep -B 2 Patricia testfile #打印匹配行及其前兩行。

southwest SW Lewis Dalsass 2.7 .8 2 18

southern SO Suan Chin 5.1 .95 4 15

southeast SE Patricia Hemenway 4.0 .7 4 17

/> grep -A 2 Patricia testfile #打印匹配行及其後兩行。

southeast SE Patricia Hemenway 4.0 .7 4 17

eastern EA TB Savage 4.4 .84 5 20

northeast NE AM Main Jr. 5.1 .94 3 13

點擊瞭解更多linux知識點

相關文章
相關標籤/搜索