grep的简单用法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了grep的简单用法相关的知识,希望对你有一定的参考价值。

过滤root,就会把关于root的过滤出来,其他不匹配的就不会过滤
[[email protected] sam]# grep ‘root‘ passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

默认是有颜色显示的,在centos 6里面默认是不带颜色的
[[email protected] sam]# which grep
alias grep=‘grep --color=auto‘
/usr /bin/grep

-c 显示一共有多少行
[[email protected] sam]# grep -c ‘root‘ passwd
2

-n 显示匹配结果分别是第几行
[[email protected] sam]# grep -n ‘root‘ passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin

-i 不区分大小写
[[email protected] 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的匹配出来
[[email protected] 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 遍历所有子目录
[[email protected] sam]# grep -r ‘root‘ passwd

-A 后面需要跟数字,匹配到之后往下再读取两行
[[email protected] 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 后面需要跟数字,匹配到之后往上再读取两行
[[email protected] 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 后面需要跟数字,匹配到之后往上下再读取两行
[[email protected] 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

[] 匹配[]里面的任意一个字符
[[email protected] 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

也可以取反,列出不带数字的行
[[email protected] sam]# grep -v ‘[0-9]‘ passwd
ROOT

匹配所有开头带#的行
[[email protected] 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

也可以取反不以#开头的行
[[email protected] sam]# grep -v ‘^#‘ inittabbr/>sadf
12
[email protected]#
aaw3
id:3:initdefault:

匹配以非数字的行,也就是取反,除了数字外其他的都是显示红色
[[email protected] 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

在[]前面再^显示以非数字开头的行
[[email protected] 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结尾的中间匹配一个字符
[[email protected] sam]# grep ‘r.o‘ passwd
root:x:0:0:root:/root:/bin/bash
2323r&o
operator:x:11:0:operator:/root:/sbin/nologin

前面这个o可以匹配多次,
[[email protected] 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

. 匹配所有的字符
[[email protected] 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 {} 是匹配的一个范围, 可以匹配两次,有两个斜杠是脱意的用法
[[email protected] 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就可以省去脱意的/
[[email protected] 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
[[email protected]lin 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的
[[email protected] sam]# grep -E ‘(oo){2}‘ passwd
vcsa:x:69:69:virtual console memory oooowner:/dev:/sbin/nologin

  • +前面的这个字符匹配一次或多次
    [[email protected] 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

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

? ?前面的这个字符重复0次或者1次
[[email protected] 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
[[email protected] 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的简单用法的主要内容,如果未能解决你的问题,请参考以下文章

cat,grep,命令总结的简单用法

centos 下 ls grep head cut简单用法

grep用法

Linux中awk命令的简单用法

awk简单用法

awk简单用法