rep / egrephtml
語法: grep [-cinvABC] 'word' filenamelinux
-c :打印符合要求的行數
-i :忽略大小寫
-n :在輸出符合要求的行的同時連同行號一塊兒輸出
-v :打印不符合要求的行
-A :後跟一個數字(有無空格均可以),例如 A2則表示打印符合要求的行以及下面兩行
-B :後跟一個數字,例如 B2 則表示打印符合要求的行以及上面兩行
-C :後跟一個數字,例如 C2 則表示打印符合要求的行以及上下各兩行正則表達式
把包含 ‘halt' 的行以及這行下面的兩行都打印出。vim
[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' 的行以及這行上面的兩行都打印出。bash
[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' 的行以及這行上面和下面的各兩行都打印出。工具
過濾出帶有某個關鍵詞的行並輸出行號 post
[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
過濾不帶有某個關鍵詞的行,並輸出行號 this
[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
過濾出全部包含數字的行 spa
[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:
過濾出全部不包含數字的行 orm
[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=/
在正則表達式中, 「^」 表示行的開始, 「$」 表示行的結尾,那麼空行則能夠用 「^$」 表示,如何打印出不以英文字母開頭的行呢?
[root@localhost ~]# vim test.txt [root@localhost ~]# cat test.txt 123 abc 456 abc2323 #laksdjf Alllllllll
先在test.txt中寫幾行字符串,用來作實驗。
[root@localhost ~]# grep '^[^a-zA-Z]' test.txt 123 456 #laksdjf
[root@localhost ~]# grep '[^a-zA-Z]' test.txt 123 456 abc2323 #laksdjf
若是是數字的話就用[0-9]這樣的形式,固然有時候也能夠用這樣的形式[15]即只含有1或者5,注意,它不會認爲是15。若是要過濾出數字以及大小寫字母則要這樣寫[0-9a-zA-Z]。另外[ ]還有一種形式,就是[^字符] 表示除[ ]內的字符以外的字符。
過濾任意一個字符與重複字符
[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
. 表示任意一個字符,上例中,就是把符合r與o之間有兩個任意字符的行過濾出來, * 表示零個或多個前面的字符。
[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' 如今你是否想到了 ‘.*' 這個組合表示什麼意義?
[root@localhost ~]# grep '.*' /etc/passwd |wc -l 27 [root@localhost ~]# wc -l /etc/passwd 27 /etc/passwd
‘.*' 表示零個或多個任意字符,空行也包含在內。
指定要過濾字符出現的次數
[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
這裏用到了{ },其內部爲數字,表示前面的字符要重複的次數。上例中表示包含有兩個o 即 ‘oo' 的行。注意,{ }左右都須要加上脫意字符 ‘\', 另外,使用{ }咱們還能夠表示一個範圍的,具體格式是 ‘{n1,n2}' 其中n1<n2,表示重複n1到n2次前面的字符,n2還能夠爲空,則表示大於等於n1次。
上面部分講的grep,另外經常用到egrep這個工具,簡單點講,後者是前者的擴展版本,咱們能夠用egrep完成grep不能完成的工做,固然了grep能完成的egrep徹底能夠完成。若是你嫌麻煩,egrep瞭解一下便可,由於grep的功能已經足夠能夠勝任你的平常工做了。下面介紹egrep不用於grep的幾個用法。爲了試驗方便,把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 '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.txtrot: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
篩選字符串1或者字符串2
[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
egrep中( )的應用
[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
用( )表示一個總體,例如(oo)+就表示1個 ‘oo' 或者多個 ‘oo'
[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