2019独角兽企业重金招聘Python工程师标准>>>
10月16日任务
9.1 正则介绍_grep上
9.2 grep中
9.3 grep下
grep基本用法
grep [-cinvABC] 'word' filename
- centos7.x版本的grep实则为grep --color=auto
- -c 显示匹配到的行数
[root@localhost ~]# cp /etc/passwd /tmp
[root@localhost ~]# cd /tmp
[root@localhost tmp]# [root@centos7 tmp]# grep -c 'root' passwd
2
- -i 忽略大小写
[root@centos7 tmp]# grep -i castiel passwd
castiel:x:1000:1000::/home/castiel:/bin/bash
Castiel:X:1000:1000::/home/castiel:/bin/bash
- -n 显示匹配行的同时显示行号
[root@centos7 tmp]# grep -n 'root' passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
- -v 对匹配信息取反
[root@centos7 tmp]# grep -iv castiel 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
.....
- -r 递归匹配过滤(对目录进行操作)
[root@centos7 tmp]# grep -r 'root' /etc
/etc/pki/ca-trust/ca-legacy.conf:# The upstream Mozilla.org project tests all changes to the root CA
/etc/pki/ca-trust/ca-legacy.conf:# to temporarily keep certain (legacy) root CA certificates trusted,
/etc/pki/ca-trust/ca-legacy.conf:# It may keep root CA certificate as trusted, which the upstream
/etc/pki/ca-trust/extracted/README:root CA certificates.
/etc/pki/ca-trust/extracted/java/README:root CA certificates.
匹配到二进制文件 /etc/pki/ca-trust/extracted/java/cacerts
/etc/pki/ca-trust/extracted/openssl/README:root CA certificates.
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt:# Comodo AAA Services root
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt:# Comodo Secure Services root
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt:# Comodo Trusted Services root
- -An(after) 显示匹配行及其后n行
[root@centos7 tmp]# grep -A2 'root' 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
--
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
- -Bn(before) 显示匹配行及其前n行
[root@centos7 tmp]# grep -B2 'root' passwd
root:x:0:0:root:/root:/bin/bash
--
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
- -Cn 显示匹配行及其前后n行
[root@centos7 tmp]# grep -C2 'root' 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
--
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
正则
# 测试内容
[root@localhost ~]# cat test.txt
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
111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#abcskhd
- 数字
[0-9]
[root@localhost ~]# grep '[0-9]' test.txt
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
111111111111111111111111111111
- 字母
[a-zA-Z]
[root@localhost ~]# grep '[a-zA-Z]' test.txt
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
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
#abcskhd
- 取反
[^]
(在方括号内才表示取反)
[root@localhost ~]# grep '[^a-zA-Z]' test.txt
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
111111111111111111111111111111
#abcskhd
- 开头
^
[root@localhost ~]# grep '^#' test.txt
#abcskhd
- 结尾
$
[root@localhost ~]# grep 'in$' test.txt
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
- 匹配任意1个字符
.
[root@localhost ~]# grep 'b.n' test.txt
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
- 匹配0或多个之前的一个字符 *
[root@localhost ~]# grep 'a*m' test.txt
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
扩展正则(grep -E 或 egrep)
- 匹配1或多个之前的一个字符
+
[root@localhost ~]# grep -E 'ro+t' test.txt
root:x:0:0:root:/root:/bin/bash
- 匹配花括号之前那个字符的个数
{n}
或{n, m}
[root@localhost ~]# grep -E 'o{2}' test.txt
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@localhost ~]# grep -E 'o{1,2}' test.txt
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
- 匹配0或1个之前的字符
?
# 匹配0或1个字符o
[root@localhost ~]# grep -E 'oo?' test.txt
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
- 匹配其中之一个表达式:
|
[root@localhost ~]# grep -E '111|aaa' test.txt
111111111111111111111111111111
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
实用grep实例
- 过滤注释行
grep -v '^#'
- 过滤空行
grep -v '^$'
- 过滤手机号
grep -E '+86-[0-9]{11}'
[root@centos7 tmp]# echo "+86-15301203401" | grep -E '+86-[0-9]{11}'
+86-15301203401
- 过滤邮箱
grep -E '\.\*@\.\*\.\.\*'
[root@centos7 tmp]# echo "12ab3#@123@qq.com" | grep -E '.*@.*\..*'
12ab3#@123@qq.com
[root@centos7 tmp]# echo "12323@163.com" | grep -E '.*@.*\..*'
12323@163.com
- 过滤出/etc目录下,包含root字符的所有的.conf文件内的行!
--include
[root@centos7 tmp]# grep -r --include="*.conf" 'root' /etc
/etc/pki/ca-trust/ca-legacy.conf:# The upstream Mozilla.org project tests all changes to the root CA
/etc/pki/ca-trust/ca-legacy.conf:# to temporarily keep certain (legacy) root CA certificates trusted,
/etc/pki/ca-trust/ca-legacy.conf:# It may keep root CA certificate as trusted, which the upstream
/etc/yum/pluginconf.d/fastestmirror.conf:# as root).
/etc/selinux/semanage.conf:ignoredirs=/root
/etc/dbus-1/system.d/teamd.conf: <policy user="root">
/etc/dbus-1/system.d/org.freedesktop.hostname1.conf: <policy user="root">
/etc/dbus-1/system.d/org.freedesktop.import1.conf: <policy user="root">
/etc/dbus-1/system.d/org.freedesktop.locale1.conf: <policy user="root">
......
- 扩展正则 ( ) 的应用
[root@localhost ~]# grep -E 'a(e|d)m' test.txt
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin