测试文件root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa
DADddd:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
a,匹配含有root的行[root@krlcgcms01 test]# grep root test
root:x:0:0:root:/root:/bin/bash
b,匹配以root开头或者以zhang开头的行,注意反斜杠[root@krlcgcms01 test]# cat test |grep '^\(root\|zhang\)'
root:x:0:0:root:/root:/bin/bash
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
c,匹配以root开头或者以zhang开头的行,注意反斜杠,根上面一个例子一样,-e默认是省去的[root@krlcgcms01 test]# cat test |grep -e '^\(root\|zhang\)'
root:x:0:0:root:/root:/bin/bash
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
d,匹配以zhang开头,只含有字母[root@krlcgcms01 test]# echo 'zhangying' |grep '^zhang[a-z]*$'
zhangying
e,匹配以bin开头的行,用的egrep,在这里可以换成-F,-G[root@krlcgcms01 test]# cat test |grep -E '^bin'
bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa
f,在匹配的行前面加上该行在文件中,或者输出中所在的行号[root@krlcgcms01 test]# cat test|grep -n zhangy
7:zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
13:ba:x:1002:1002::/home/zhangy:/bin/bash
15:@zhangying:*:1004:1004::/home/test:/bin/bash
g,不匹配以bin开头的行,并显示行号[root@krlcgcms01 test]# cat test|grep -nv '^bin'
root:x:0:0:root:/root:/bin/bash
DADddd:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po
h,显示匹配的个数,不显示内容[root@krlcgcms01 test]# cat test|grep -c zhang
3
i,匹配system,没有加-i没有匹配到东西。[root@krlcgcms01 test]# grep system test
[root@krlcgcms01 test]# grep -ni system test
9:dbus:x:81:81:System message bus:/:/bin/false
j,匹配zhan没有匹配到东西,匹配zhangy能匹配到,因为在test文件中,有zhangy这个单词[root@krlcgcms01 test]# cat test|grep -w zhan
[root@krlcgcms01 test]# cat test|grep -w zhangy
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
k,在这里-x后面东西,和输出中的整行相同时,才会输出[root@krlcgcms01 test]# echo "aaaaaa" |grep -x aaa
[root@krlcgcms01 test]# echo "aaaa" |grep -x aaaa
aaaa
l,最多只匹配一次,如果把-m 1去掉的话,会有三个[root@krlcgcms01 test]# cat test |grep -m 1 zhang
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
m,匹配行的前面显示块号,这个块号是干什么的,不知道,有谁知道可否告诉我一下[apacheuser@krlcgcms01 test]$ cat test |grep -b zha
241:zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
480:ba:x:1002:1002::/home/zhangy:/bin/bash
558:@zhangying:*:1004:1004::/home/test:/bin/bash
n,多文件匹配时,在匹配的行前面加上文件名[apacheuser@krlcgcms01 test]$ grep -H 'root' test test2 testbak
test:root:x:0:0:root:/root:/bin/bash
test2:root
testbak:root:x:0:0:root:/root:/bin/bash
o,多文件匹配时,在匹配的行前面不加上文件名[apacheuser@krlcgcms01 test]$ grep -h 'root' test test2 testbak
root:x:0:0:root:/root:/bin/bash
root
root:x:0:0:root:/root:/bin/bash
p,多文件匹配时,显示匹配文件的文件名[apacheuser@krlcgcms01 test]$ grep -l 'root' test test2 testbak DAta
test
test2
testbak
q,没有-o时,有一行匹配,这一行里面有3个root,加上-o后,这个3个root就出来了[apacheuser@krlcgcms01 test]$ grep 'root' test
root:x:0:0:root:/root:/bin/bash
[apacheuser@krlcgcms01 test]$ grep -o 'root' test
root
root
root
r,递归显示匹配的内容,在test目录下面建个mytest目录,copy test目录下面的test文件到mytest下面,能看到上面的结果[root@krlcgcms01 test]# grep test -R /tmp/test/mytest
/tmp/test/mytest/test:test:x:1003:1003::/home/test:/bin/bash
/tmp/test/mytest/test:@zhangying:*:1004:1004::/home/test:/bin/bash
s,显示匹配root后面的3行[root@krlcgcms01 test]# cat test |grep -A 3 root
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false,aaa,bbbb,cccc,aaaaaa
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false