grep工具介绍:grep&egrep工具作为shell必备三剑客之一:
grep: (global search regular expression and print out the line),使用正则表达式搜索文本并把行打印出来),是一种强大的文本搜索工具,它可以搜索文本,并把需要的行打印出来:
语法:grep [ options ] "word" filename
Options:
-c:=count 行数
-i:=ignore-case 不区分大小写
-n:=line-number 显示行号
-v:=revert-match 取反
-r,-R:=recursive(递归的,循环的)读取目录下的所有文件,包括子目录
-A:后面跟数字,过滤出复合要求的行以及下面n行
-B:同上,过滤出符合要求的行以及上面n行 -C:同上,同时过滤出符合要求的行以及上下各n行
-E:‘grep -E’ = egrep
eg:
过滤一个字符串
[root@adai003 grep]# grep 'nologin' passwdbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@adai003 grep]# which grepalias grep='grep --color=auto' /usr/bin/grep
在Linux中,grep默认带color选项:
grep -c
[root@adai003 grep]# grep -c 'nologin' passwd4[root@adai003 grep]# cat !$cat passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
grep -n
[root@adai003 grep]# grep -n 'nologin' passwd2:bin:x:1:1:bin:/bin:/sbin/nologin3:daemon:x:2:2:daemon:/sbin:/sbin/nologin4:adm:x:3:4:adm:/var/adm:/sbin/nologin5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
grep -i
[root@adai003 grep]# cat !$cat passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/Nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@adai003 grep]# grep 'nologin' passwddaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@adai003 grep]# grep -i 'nologin' passwdbin:x:1:1:bin:/bin:/sbin/Nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@adai003 grep]# grep -c 'nologin' passwd3[root@adai003 grep]# grep -ic 'nologin' passwd4
grep -v
[root@adai003 grep]# grep -v 'nologin' passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/Nologin
grep -r/R
[root@adai003 grep]# tree. ├── grep1 │ └── passwd1 └── passwd [root@adai003 grep]# grep -r 'root' ../passwd:root:x:0:0:root:/root:/bin/bash./grep1/passwd1:root:x:0:0:root:/root:/bin/bash[root@adai003 grep]# grep -R 'root' ../passwd:root:x:0:0:root:/root:/bin/bash./grep1/passwd1:root:x:0:0:root:/root:/bin/bash
grep -A
[root@adai003 grep]# grep -nA2 'daemon' passwd3:daemon:x:2:2:daemon:/sbin:/sbin/nologin4-adm:x:3:4:adm:/var/adm:/sbin/nologin5-lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
grep -B
[root@adai003 grep]# grep -nB2 'daemon' passwd1-root:x:0:0:root:/root:/bin/bash2-bin:x:1:1:bin:/bin:/sbin/Nologin3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
grep -C
[root@adai003 grep]# grep -nC2 'daemon' passwd1-root:x:0:0:root:/root:/bin/bash2-bin:x:1:1:bin:/bin:/sbin/Nologin3:daemon:x:2:2:daemon:/sbin:/sbin/nologin4-adm:x:3:4:adm:/var/adm:/sbin/nologin5-lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
9.2 grep(中)
grep应用
eg1:grep -n '[0-9]'
[root@adai003 grep]# grep -n '[0-9]' passwd1:root:x:0:0:root:/root:/bin/bash2:bin:x:1:1:bin:/bin:/sbin/Nologin3:daemon:x:2:2:daemon:/sbin:/sbin/nologin5:adm:x:3:4:adm:/var/adm:/sbin/nologin6:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin7:pcp:x:996:994:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
eg2:grep -nv '[0-9]'
[root@adai003 grep]# cat !$cat passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/Nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinCtrl-Alt-Delete is handled by /usr/lib/systemd/system/cadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinpcp:x:996:994:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target [root@adai003 grep]# grep -nv '[0-9]' passwd4:Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/c8:Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
grep -n ' ^# ' 过滤以#开头的行(^在此表示以某字符开头)
[root@adai003 grep]# cat inittab# inittab is no longer used when using systemd. # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. lhugalkdoljweriii # systemd uses 'targets' instead of runlevels. By default, there are two main targets: jlkdghkhladlllsdjfj # multi-user.target: analogous to runlevel 3# graphical.target: analogous to runlevel 5[root@adai003 grep]# grep -n '^#' inittab1:# inittab is no longer used when using systemd.2:# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.4:# systemd uses 'targets' instead of runlevels. By default, there are two main targets:6:# multi-user.target: analogous to runlevel 37:# graphical.target: analogous to runlevel 5[root@adai003 grep]# grep -nv '^#' inittab 过滤不是以#开头的行3:lhugalkdoljweriii5:jlkdghkhladlllsdjfj
grep '[^0-9]' 表示非0-9中任意字符所在的行,即任意非数字所在的行(只要包含非数字字符都算在内,中括号内的‘^’表示否定)
[root@adai003 grep]# cat inittab# inittab is no longer used when using systemd. # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. lhugalkdoljweriii235425241245a879869 # systemd uses 'targets' instead of runlevels. By default, there are two main targets:6999%&*0927309# multi-user.target: analogous to runlevel 3^%^%$&%%*&## # graphical.target: analogous to runlevel 5[root@adai003 grep]# grep '[^0-9]' inittab # inittab is no longer used when using systemd. # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. lhugalkdoljweriii1245a879869 # systemd uses 'targets' instead of runlevels. By default, there are two main target:6999%&*0927309# multi-user.target: analogous to runlevel 3^%^%$&%%*&## # graphical.target: analogous to runlevel 5
grep '^[^0-9]' 表示以非数字字符开头的行
[root@adai003 grep]# grep '^[^0-9]' inittab# inittab is no longer used when using systemd.# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.lhugalkdoljweriii# systemd uses 'targets' instead of runlevels. By default, there are two main targets:# multi-user.target: analogous to runlevel 3^%^%$&%%*&### graphical.target: analogous to runlevel 5
grep 'r.o' 在此‘.’表示一个任意字符
[root@adai003 grep]# grep 'r.o' passwdroot:x:0:0:roprot:/root:/bin/bashpcp:x:996:994:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
grep 'o*o' 在此‘ * ’表示其左边的字符重复n此(n≥0)
[root@adai003 grep]# grep 'o*o' passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/Nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinCtrl-Alt-Deoooleooote ios handled by /usr/lib/systemd/system/cadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinpcp:x:996:994:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
grep '.*' 在此' . * '组合表示所有任意字符
[root@adai003 grep]# grep '.*' passwdroot:x:0:0:roprot:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/Nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinCtrl-Alt-Deoooleooote ios handled by /usr/lib/systemd/system/cadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinpcp:x:996:994:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
grep 'o\ {2\ }' 在此{}表示前面字符或字符串出现的次数(也可以是一个区间,如'o\ {0,3\ }'表示0个到3个o)
[root@adai003 grep]# grep -n 'o\{2\}' passwd1:root:x:0:0:roprot:/root:/bin/bash4:Ctrl-Alt-Deoooleooote ios handled by /usr/lib/systemd/system/c6:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@adai003 grep]# grep -n 'o\{3\}' passwd4:Ctrl-Alt-Deoooleooote ios handled by /usr/lib/systemd/system/c [root@adai003 grep]# grep -n 'o\{0,3\}' passwd1:root:x:0:0:roprot:/root:/bin/bash2:bin:x:1:1:bin:/bin:/sbin/Nologin3:daemon:x:2:2:daemon:/sbin:/sbin/nologin4:Ctrl-Alt-Deoooleooote ios handled by /usr/lib/systemd/system/c5:adm:x:3:4:adm:/var/adm:/sbin/nologin6:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin7:pcp:x:996:994:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin8:Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
9.3 grep(下)
egrep应用
egrep 'o{2}'=grep -E 'o {2}' = grep 'o\ {2\ }'
[root@adai003 grep]# grep -nE 'o{2}' passwd1:root:x:0:0:roprot:/root:/bin/bash4:Ctrl-Alt-Deoooleooote ios handled by /usr/lib/systemd/system/c6:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@adai003 grep]# egrep -n 'o{2}' passwd1:root:x:0:0:roprot:/root:/bin/bash4:Ctrl-Alt-Deoooleooote ios handled by /usr/lib/systemd/system/c6:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin当{}前面是一个字符串时需要使用‘()’
egrep -n 'o+o' 在此‘+’表示其左边的字符重复n此(n≥1,注意和*的区别)
[root@adai003 grep]# egrep -n 'o+o' passwd1:root:x:0:0:roprot:/root:/bin/bash4:Ctrl-Alt-Deoooleooote ios handled by /usr/lib/systemd/system/c6:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
egrep -n 'o?t' 在此?表示其前面的字符出现0次或1次
[root@adai003 grep]# egrep -n 'o?t' passwd1:root:x:0:0:roprot:/root:/bin/bash4:Ctrl-Alt-Deoooleooote ios handled by /usr/lib/systemd/system/c7:pcp:x:996:994:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin8:Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
egrep -n 'root|nologin' 在此‘|’表示或者
[root@adai003 grep]# egrep -n 'root|nologin' passwd1:root:x:0:0:roprot:/root:/bin/bash3:daemon:x:2:2:daemon:/sbin:/sbin/nologin5:adm:x:3:4:adm:/var/adm:/sbin/nologin6:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin7:pcp:x:996:994:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin
grep -nE '(oo){2}' 在此()内的内容表示一个字符组合,即连续出现两次‘oo’=‘oooo’字符串
[root@adai003 grep]# grep -nE '(oo){2}' passwd4:Ctrl-Alt-Deoooleoooote ios handled by /usr/lib/systemd/system/c
扩展
过滤一个目录下所有某类型文档中含有某字符的行:
[root@adai003 ~]# grep -r --include="*.txt" 'root' ./sed/./sed/test.txt:root:x:0:0:roprot:/root:/bin/bash
说明: 过滤./sed/目录下所有.txt文件中含有字符串root的行。