新聞中心
rep / egrep

目前創(chuàng)新互聯(lián)已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁(yè)空間、成都網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、高淳網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
語(yǔ)法: grep [-cinvABC] ‘word’ filename
-c :打印符合要求的行數(shù)
-i :忽略大小寫
-n :在輸出符合要求的行的同時(shí)連同行號(hào)一起輸出
-v :打印不符合要求的行
-A :后跟一個(gè)數(shù)字(有無(wú)空格都可以),例如 A2則表示打印符合要求的行以及下面兩行
-B :后跟一個(gè)數(shù)字,例如 B2 則表示打印符合要求的行以及上面兩行
-C :后跟一個(gè)數(shù)字,例如 C2 則表示打印符合要求的行以及上下各兩行
把包含 ‘halt’ 的行以及這行下面的兩行都打印出。
[root@localhost ~]# grep -A2 'halt' /etc/passwd halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
把包含 ‘halt’ 的行以及這行上面的兩行都打印出。
[root@localhost ~]# grep -B2 'halt' /etc/passwd sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt
把包含 ‘halt’ 的行以及這行上面和下面的各兩行都打印出。
過(guò)濾出帶有某個(gè)關(guān)鍵詞的行并輸出行號(hào)
[root@localhost ~]# grep -n 'root' /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 11:operator:x:11:0:operator:/root:/sbin/nologin
過(guò)濾不帶有某個(gè)關(guān)鍵詞的行,并輸出行號(hào)
[root@localhost ~]# grep -nv 'nologin' /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 6:sync:x:5:0:sync:/sbin:/bin/sync 7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8:halt:x:7:0:halt:/sbin:/sbin/halt 26:test:x:511:511::/home/test:/bin/bash 27:test1:x:512:511::/home/test1:/bin/bash
過(guò)濾出所有包含數(shù)字的行
[root@localhost ~]# grep '[0-9]' /etc/inittab # upstart works, see init(5), init(8), and initctl(8). # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, without NFS (The same as 3, if you do not have networking) # 3 - Full multiuser mode # 4 - unused # 5 - X11 # 6 - reboot (Do NOT set initdefault to this) id:3:initdefault:
過(guò)濾出所有不包含數(shù)字的行
[root@localhost ~]# grep -v '[0-9]' /etc/inittab # inittab is only used by upstart for the default runlevel. # # ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # System initialization is started by /etc/init/rcS.conf # # Individual runlevels are started by /etc/init/rc.conf # # Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf # # Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf, # with configuration in /etc/sysconfig/init. # # For information on how to write upstart event handlers, or how # # Default runlevel. The runlevels used are: #
把所有以 ‘#’ 開頭的行去除
[root@localhost ~]# grep -v '^#' /etc/inittab id:3:initdefault:
去除所有空行和以 ‘#’ 開頭的行
[root@localhost ~]# grep -v '^#' /etc/crontab |grep -v '^$' SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/
在正則表達(dá)式中, “^” 表示行的開始, “$” 表示行的結(jié)尾,那么空行則可以用 “^$” 表示,如何打印出不以英文字母開頭的行呢?
[root@localhost ~]# vim test.txt [root@localhost ~]# cat test.txt 123 abc 456 abc2323 #laksdjf Alllllllll
先在test.txt中寫幾行字符串,用來(lái)做實(shí)驗(yàn)。
[root@localhost ~]# grep '^[^a-zA-Z]' test.txt 123 456 #laksdjf
[root@localhost ~]# grep '[^a-zA-Z]' test.txt 123 456 abc2323 #laksdjf
如果是數(shù)字的話就用[0-9]這樣的形式,當(dāng)然有時(shí)候也可以用這樣的形式[15]即只含有1或者5,注意,它不會(huì)認(rèn)為是15。如果要過(guò)濾出數(shù)字以及大小寫字母則要這樣寫[0-9a-zA-Z]。另外[ ]還有一種形式,就是[^字符] 表示除[ ]內(nèi)的字符之外的字符。
過(guò)濾任意一個(gè)字符與重復(fù)字符
[root@localhost ~]# grep 'r..o' /etc/passwd operator:x:11:0:operator:/root:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
. 表示任意一個(gè)字符,上例中,就是把符合r與o之間有兩個(gè)任意字符的行過(guò)濾出來(lái), * 表示零個(gè)或多個(gè)前面的字符。
[root@localhost ~]# grep 'ooo*' /etc/passwd root:x:0:0:root:/root:/bin/bash lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin
‘ooo*’ 表示oo, ooo, oooo … 或者更多的 ‘o’ 現(xiàn)在你是否想到了 ‘.*’ 這個(gè)組合表示什么意義?
[root@localhost ~]# grep '.*' /etc/passwd |wc -l 27 [root@localhost ~]# wc -l /etc/passwd 27 /etc/passwd
‘.*’ 表示零個(gè)或多個(gè)任意字符,空行也包含在內(nèi)。
指定要過(guò)濾字符出現(xiàn)的次數(shù)
[root@localhost ~]# grep 'o\{2\}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin也可以不用脫意符\ 加上-E
grep -E 'o{2}' /etc/passwd這里用到了{(lán) },其內(nèi)部為數(shù)字,表示前面的字符要重復(fù)的次數(shù)。上例中表示包含有兩個(gè)o 即 ‘oo’ 的行。注意,{ }左右都需要加上脫意字符 ‘\’, 另外,使用{ }我們還可以表示一個(gè)范圍的,具體格式是 ‘{n1,n2}’ 其中n1 上面部分講的grep,另外常常用到egrep這個(gè)工具,簡(jiǎn)單點(diǎn)講,后者是前者的擴(kuò)展版本,我們可以用egrep完成grep不能完成的工作,當(dāng)然了grep能完成的egrep完全可以完成。如果你嫌麻煩,egrep了解一下即可,因?yàn)間rep的功能已經(jīng)足夠可以勝任你的日常工作了。下面介紹egrep不用于grep的幾個(gè)用法。為了試驗(yàn)方便,把test.txt 編輯成如下內(nèi)容: 篩選一個(gè)或一個(gè)以上前面的字符 篩選零個(gè)或一個(gè)前面的字符 篩選字符串1或者字符串2 egrep中( )的應(yīng)用 用( )表示一個(gè)整體,例如(oo)+就表示1個(gè) ‘oo’ 或者多個(gè) ‘oo’ 本篇文章到此結(jié)束,如果您有相關(guān)技術(shù)方面疑問可以聯(lián)系我們技術(shù)人員遠(yuǎn)程解決,感謝大家支持本站! 成都創(chuàng)新互聯(lián)建站主營(yíng):成都網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、網(wǎng)站改版的網(wǎng)站建設(shè)公司,提供成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、成都網(wǎng)站推廣、成都網(wǎng)站優(yōu)化seo、響應(yīng)式移動(dòng)網(wǎng)站開發(fā)制作等網(wǎng)站服務(wù)。
rot:x:0:0:/rot:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash
1111111111111111111111111111111
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
[root@localhost ~]# egrep 'o+' test.txt
rot:x:0:0:/rot:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash
[root@localhost ~]# egrep 'oo+' test.txt
operator:x:11:0:operator:/root:/sbin/nologin
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash
[root@localhost ~]# egrep 'ooo+' test.txt
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash
[root@localhost ~]# egrep 'o?' test.txt
rot:x:0:0:/rot:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash
1111111111111111111111111111111
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
[root@localhost ~]# egrep 'ooo?' test.txt
operator:x:11:0:operator:/root:/sbin/nologin
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash
[root@localhost ~]# egrep 'oooo?' test.txt
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash
[root@localhost ~]# egrep 'aaa|111|ooo' test.txt
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash
1111111111111111111111111111111
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
[root@localhost ~]# egrep 'r(oo)|(at)o' test.txt
operator:x:11:0:operator:/root:/sbin/nologin
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash
[root@localhost ~]# egrep '(oo)+' test.txt
operator:x:11:0:operator:/root:/sbin/nologin
operator:x:11:0:operator:/rooot:/sbin/nologin
roooot:x:0:0:/rooooot:/bin/bash
當(dāng)前標(biāo)題:Linux中g(shù)rep和egrep命令詳解
網(wǎng)頁(yè)網(wǎng)址:http://fisionsoft.com.cn/article/cdiggec.html


咨詢
建站咨詢
