grep的简单用法

过滤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 '^#' inittabbr/>sadf
12
!@#
aaw3
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

转载于:https://blog.51cto.com/huazi1991/2086225

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值