1. 程式人生 > >grep的簡單用法

grep的簡單用法

grep、egrep

過濾root,就會把關於root的過濾出來,其他不匹配的就不會過濾
[root@kylin sam]# grep ‘root‘ passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

默認是有顏色顯示的,在centos 6裏面默認是不帶顏色的
[root@kylin sam]# which grep
alias grep=‘grep --color=auto‘
/usr /bin/grep

-c 顯示一共有多少行
[root@kylin sam]# grep -c ‘root‘ passwd
2

-n 顯示匹配結果分別是第幾行

[root@kylin sam]# grep -n ‘root‘ passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin

-i 不區分大小寫
[root@kylin sam]# grep -in ‘root‘ passwd
1:root:x:0:0:root:/root:/bin/bash
3:ROOT
12:operator:x:11:0:operator:/root:/sbin/nologin

-v 取反,就是把不包含root的匹配出來
[root@kylin sam]# grep -v ‘root‘ passwd

bin:x:1:1:bin:/bin:/sbin/nologin
ROOT
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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

-r 遍歷所有子目錄
[root@kylin sam]# grep -r ‘root‘ passwd

-A 後面需要跟數字,匹配到之後往下再讀取兩行
[root@kylin sam]# grep -A2 ‘mail‘ passwd
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

-B 後面需要跟數字,匹配到之後往上再讀取兩行
[root@kylin sam]# grep -B2 ‘mail‘ passwd
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

-C 後面需要跟數字,匹配到之後往上下再讀取兩行
[root@kylin sam]# grep -C2 ‘mail‘ passwd
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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

[] 匹配[]裏面的任意一個字符
[root@kylin sam]# grep ‘[0-9]‘ passwd
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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

也可以取反,列出不帶數字的行
[root@kylin sam]# grep -v ‘[0-9]‘ passwd
ROOT

匹配所有開頭帶#的行
[root@kylin sam]# grep ‘^#‘ 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

也可以取反不以#開頭的行
[root@kylin sam]# grep -v ‘^#‘ inittabsadf
12
!@#
br/>sadf
12
!@#
id:3:initdefault:

匹配以非數字的行,也就是取反,除了數字外其他的都是顯示紅色
[root@kylin sam]# grep ‘[^0-9]‘ 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

在[]前面再^顯示以非數字開頭的行
[root@kylin sam]# grep ‘^[^0-9]‘ 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

#

. 可以匹配以r開頭o結尾的中間匹配一個字符
[root@kylin sam]# grep ‘r.o‘ passwd
root:x:0:0:root:/root:/bin/bash
2323r&o
operator:x:11:0:operator:/root:/sbin/nologin

前面這個o可以匹配多次,
[root@kylin sam]# grep ‘o
o‘ passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

. 匹配所有的字符
[root@kylin sam]# grep ‘.
‘ passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
ROOT
daemon:x:2:2:daemon:/sbin:/sbin/nologin
sdffsdr00o12
2323r&o
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

o {} 是匹配的一個範圍, 可以匹配兩次,有兩個斜杠是脫意的用法
[root@kylin sam]# grep ‘o{2}‘ 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
vcsa:x:69:69:virtual console memory oooowner:/dev:/sbin/nologin
postfix:x:89:89::/var/spoool/postfix:/sbin/nologin

grep -E =egrep 用-E就可以省去脫意的/
[root@kylin sam]# grep -E ‘o{2}‘ 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
vcsa:x:69:69:virtual console memory oooowner:/dev:/sbin/nologin
postfix:x:89:89::/var/spoool/postfix:/sbin/nologin
[root@kylin sam]# egrep ‘o{2}‘ 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
vcsa:x:69:69:virtual console memory oooowner:/dev:/sbin/nologin
postfix:x:89:89::/var/spoool/postfix:/sbin/nologin

用()括起來表示一個整體匹配有兩個oo的
[root@kylin sam]# grep -E ‘(oo){2}‘ passwd
vcsa:x:69:69:virtual console memory oooowner:/dev:/sbin/nologin

  • +前面的這個字符匹配一次或多次
    [root@kylin sam]# grep -E ‘o+o‘ 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
    vcsa:x:69:69:virtual console memory oooowner:/dev:/sbin/nologin
    postfix:x:89:89::/var/spoool/postfix:/sbin/nologin

也可以匹配其他的字符
[root@kylin sam]# grep -E ‘o+b‘ passwd
nobody:x:99:99:Nobody:/:/sbin/nologin

? ?前面的這個字符重復0次或者1次
[root@kylin sam]# grep -E ‘o?t‘ passwd
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
vcsa:x:69:69:virtual console memory oooowner:/dev:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spoool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

| 或者,匹配root或者nobody
[root@kylin sam]# grep -E ‘root|nobody‘ passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin

grep的簡單用法