1. 程式人生 > 實用技巧 >Linux中grep和egrep命令詳解

Linux中grep和egrep命令詳解

rep / egrep

語法: grep [-cinvABC] 'word' filename

-c :列印符合要求的行數
-i :忽略大小寫
-n :在輸出符合要求的行的同時連同行號一起輸出
-v :列印不符合要求的行
-A :後跟一個數字(有無空格都可以),例如 A2則表示列印符合要求的行以及下面兩行
-B :後跟一個數字,例如 B2 則表示列印符合要求的行以及上面兩行
-C :後跟一個數字,例如 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' 的行以及這行上面和下面的各兩行都打印出。

過濾出帶有某個關鍵詞的行並輸出行號

[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

過濾不帶有某個關鍵詞的行,並輸出行號

[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

過濾出所有包含數字的行

[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:

過濾出所有不包含數字的行

[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.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

篩選字串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

以上就是本次介紹的Linux之grep和egrep命令的全部相關知識點,感謝大家的學習和對碼農教程的支援。