grep家族:
1. grep退出狀態(tài):
0: 表示成功;
1: 表示在所提供的文件無法找到匹配的pattern;
2: 表示參數(shù)中提供的文件不存在。
見如下示例:
/> grep 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
/> echo $?
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中應(yīng)用正則表達(dá)式的實(shí)例:
需要說明的是下面所涉及的正則表達(dá)式在上一篇中已經(jīng)給出了詳細(xì)的說明,因此在看下面例子的時(shí)候艇潭,可以與前一篇的正則說明部分結(jié)合著看。
/> 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結(jié)尾的行。
northwest NW Charles Main 3.0 .98 3 34
/> grep '5\..' testfile #打印出第一個(gè)字符是5辛臊,后面跟著一個(gè).字符,在后面是任意字符的行房交。
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 #打印出所有包含前兩個(gè)字符是大寫字符刃唤,后面緊跟一個(gè)空格及一個(gè)大寫字母的行。
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
注:在執(zhí)行以上命令時(shí)白群,如果不能得到預(yù)期的結(jié)果尚胞,即grep忽略了大小寫,導(dǎo)致這一問題的原因很可能是當(dāng)前環(huán)境的本地化的設(shè)置問題帜慢。對于以上命令笼裳,如果我將當(dāng)前語言設(shè)置為en_US的時(shí)候唯卖,它會打印出所有的行,當(dāng)我將其修改為中文環(huán)境時(shí)躬柬,就能得到我現(xiàn)在的輸出了拜轨。
/> export LANG=zh_CN #設(shè)置當(dāng)前的語言環(huán)境為中文。
/> export LANG=en_US #設(shè)置當(dāng)前的語言環(huán)境為美國允青。
/> export LANG=en_Br #設(shè)置當(dāng)前的語言環(huán)境為英國橄碾。
/> grep '[a-z]\{9\}' testfile #打印所有包含每個(gè)字符串至少有9個(gè)連續(xù)小寫字符的字符串的行。
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
#第一個(gè)字符是3颠锉,緊跟著一個(gè)句點(diǎn)法牲,然后是任意一個(gè)數(shù)字,然后是任意個(gè)任意字符琼掠,然后又是一個(gè)3拒垃,然后是制表符,然后又是一個(gè)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 #第一個(gè)字符是n,后面是任意字母或者數(shù)字姥宝。
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. 擴(kuò)展grep(grep -E 或者 egrep):
使用擴(kuò)展grep的主要好處是增加了額外的正則表達(dá)式元字符集翅萤。下面我們還是繼續(xù)使用實(shí)例來演示擴(kuò)展grep。
/> egrep 'NW|EA' testfile #打印所有包含NW或EA的行腊满。如果不是使用egrep套么,而是grep,將不會有結(jié)果查出碳蛋。
northwest NW Charles Main 3.0 .98 3 34
eastern EA TB Savage 4.4 .84 5 20
/> grep 'NW\|EA' testfile #對于標(biāo)準(zhǔn)grep胚泌,如果在擴(kuò)展元字符前面加\,grep會自動啟用擴(kuò)展選項(xiàng)-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條命令將會打印出相同的結(jié)果玷室,即所有包含一個(gè)或多個(gè)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個(gè)或1個(gè)點(diǎn)穷缤,后面再是0和9之間的數(shù)字。
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個(gè)命令返回相同結(jié)果箩兽,即打印一個(gè)或者多個(gè)連續(xù)的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 #首先是一個(gè)或者多個(gè)字母,緊跟著一個(gè)或者多個(gè)非字母數(shù)字汗贫,最后一個(gè)是ABC中的一個(gè)身坐。
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個(gè)命令返回相同結(jié)果秸脱,即以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的值,后面緊跟著任意數(shù)量的任意字符搪花,最后還有一個(gè)es出現(xiàn)在該行遏片。
northwest NW Charles Main 3.0 .98 3 34
4. grep選項(xiàng):
這里先列出grep常用的命令行選項(xiàng):
選項(xiàng)說明-c只顯示有多少行匹配,而不具體顯示匹配的行撮竿。-h不顯示文件名吮便。-i在字符串比較的時(shí)候忽略大小寫。-l只顯示包含匹配模板的行的文件名清單幢踏。-L只顯示不包含匹配模板的行的文件名清單髓需。-n在每一行前面打印改行在文件中的行數(shù)。-v反向檢索房蝉,只顯示不匹配的行僚匆。-w只顯示完整單詞的匹配。-x只顯示完整行的匹配搭幻。-r/-R如果文件參數(shù)是目錄咧擂,該選項(xiàng)將遞歸搜索該目錄下的所有子目錄和文件。
/> grep -n '^south' testfile #-n選項(xiàng)在每一個(gè)匹配行的前面打印行號檀蹋。
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選項(xiàng)關(guān)閉了大小寫敏感松申。
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只打印整個(gè)單詞匹配的行桌肴。
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