服裝店網(wǎng)站模板網(wǎng)站快速建站
一.grep簡(jiǎn)介
grep 全程Globally search a Regular Expression and Print,是一種強(qiáng)大的文本搜索工具,它能使用特定模式匹配(包括正則表達(dá)式)搜索文本,并默認(rèn)輸出匹配行。Unix的grep家族包括grep和egrep
二.grep的工作模式
2.1.grep執(zhí)行語(yǔ)法
grep [option] file...
2.2.工作方式
- grep在一個(gè)或者多個(gè)文件中搜索字符串模板
-
如果模板中包括空格,需要使用引號(hào)引起來(lái)
-
模板后的所有字符串會(huì)被看作是文件名
2.3.工作結(jié)果
- 如果模板搜索成功,則返回0狀態(tài)碼
- 如果搜索不成功,則返回1狀態(tài)碼
- 如果搜索的文件不存在,則返回2的狀態(tài)碼。
三.grep的常用參數(shù)詳解
3.1.常用參數(shù)詳解
?3.2.演示示例
a)建立測(cè)試文件
[root@haha shells]# vim testfile
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
test:ROOT:test
test:chroot:test
test:test:root
b)過(guò)濾文件中所有含有root字符串的行
[root@haha shells]# grep root testfile
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
test:chroot:test
test:test:root
c )過(guò)濾包含 root 單詞的行
[root@haha shells]# grep root testfile -w
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
test:test:root
d )過(guò)濾含有 root 字符的行忽略大小寫(xiě)
[root@haha shells]# grep root testfile -i
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
test:ROOT:test
test:chroot:test
test:test:root
e )過(guò)濾含有 root 單詞和 bash 單詞的行
[root@haha shells]# grep -w -e root -e bash testfile
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
test:test:root
f )只顯示匹配內(nèi)容不不顯示其他并標(biāo)注行號(hào)
[root@timinglee shell]# grep -on root testfile
1:root
1:root
1:root
10:root
11:root
13:root
14:root
g )顯示包含 root 字符串的行的總數(shù)
[root@haha shells]# grep -c root testfile
5
h )顯示不含有 root 的行
[root@haha shells]# grep -v root testfile
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
j )顯示含有 adm 關(guān)鍵字及其周?chē)?/span> 2 行
[root@haha shells]# grep -2 adm testfile
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
k)顯示含有 adm 關(guān)鍵字及其下面 1 行
[root@haha shells]# grep -A1 adm testfile
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
l )顯示含有 adm 關(guān)鍵字及其上面 2 行
[root@haha shells]# grep -B2 adm testfile
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
四.正則表達(dá)式的使用
4.1.什么是正則表達(dá)式
正則表達(dá)式就是用在字符串的處理上面的一項(xiàng)表示式
在我們做程序設(shè)計(jì)時(shí)很多需要對(duì)一類(lèi)字符進(jìn)行處理,正則表達(dá)式就完美的解決了這個(gè)問(wèn)題
4.2.正則表達(dá)式?
基本正則表達(dá)式( Basic Regular Expression , BRE ),又稱(chēng)為標(biāo)準(zhǔn)正則表達(dá)式,是最早制訂的正則表達(dá)式規(guī)范,僅支持最基本的元字符集?;菊齽t表達(dá)式是POSIX 規(guī)范制訂的兩種正則表達(dá)式語(yǔ)法標(biāo)準(zhǔn)之一,另外一種語(yǔ)法標(biāo)準(zhǔn)稱(chēng)為擴(kuò)展正則表達(dá)式
擴(kuò)展正則表達(dá)式( Extended Regular Expression , ERE )支持比基本正則表達(dá)式更多的元字符
?
在grep命令使用基本正則表達(dá)式時(shí)不需要加任何參數(shù)
在grep命令使用擴(kuò)展正則表達(dá)式時(shí)必須加-E參數(shù)
示例:
a )過(guò)濾以 root 開(kāi)頭的行,過(guò)濾以 root 開(kāi)頭 bash 結(jié)尾的行
[root@haha shells]# grep ^root testfile
root:x:0:0:root:/root:/bin/bash
[root@haha shells]# grep -E "(^root|bash$)" testfile
root:x:0:0:root:/root:/bin/bash
[root@haha shells]#
b )過(guò)濾以 root 結(jié)尾的行
[root@haha shells]# grep root$ testfile
test:test:root
c )搜索 e 開(kāi)頭 y 結(jié)尾的三字符單詞,搜索 e 開(kāi)頭 y 結(jié)尾的四字符單詞
[root@haha shells]# grep e.y testfile1
eay
eby
[root@haha shells]# grep e..y testfile1
eaay
ebby
eaby
d )搜索 e 開(kāi)頭 Y 結(jié)尾的所有行
[root@haha shells]# grep e.*y testfile1
eay
eby
eaay
ebby
eaaay
ebbby
eaaaaay
ebbbbby
eababy
eaby
eabababy
e)搜索自定次數(shù)出現(xiàn)的字符
[root@haha shells]# grep -E "e.{2}y" testfile1
eaay
ebby
eaby
[root@haha shells]# grep -E "e.{,2}y" testfile1
eay
eby
eaay
ebby
eaby
[root@haha shells]# grep -E "e.{2,}y" testfile1
eaay
ebby
eaaay
ebbby
eaaaaay
ebbbbby
eababy
eaby
eabababy
[root@haha shells]# grep -E "e.+y" testfile1
eay
eby
eaay
ebby
eaaay
ebbby
eaaaaay
ebbbbby
eababy
eaby
eabababy
[root@haha shells]# grep -E "e.?y" testfile1
eay
eby
[root@haha shells]# grep -E "e[ab]y" testfile1
eay
eby
[root@haha shells]# grep -E "e[^ac]y" testfile1
eby
[root@haha shells]# grep -E "e(ab)+y" testfile1
eababy
eaby
eabababy
[root@haha shells]# grep -E "e(ab)?y" testfile1
eaby
4.3.正則表達(dá)式字符集
示例:
[root@haha 1]# touch easyalee easyAlee "easy lee" "easy@lee" "easy8lee"
[root@haha 1]# rm -rf easy[[:digit:]]lee
[root@haha 1]# rm -rf easy[[:space:]]lee
[root@haha 1]# rm -rf easy[[:lower:]]lee
[root@haha 1]# rm -rf easy[[:upper:]]lee
[root@haha 1]# rm -rf easy[[:graph:]]lee
[root@haha 1]# rm -rf easy[[:alpha:]]lee