正则之grep

来源:互联网 发布:golang package 编辑:程序博客网 时间:2024/05/16 19:20

9.1 正则介绍_grep上
grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

grep -c “sbin” /root/passwd #-c显示一共出现了多少行数

[root@wwlinux701 ~]# grep -c "sbin" /root/passwd 22

grep -i “root” /root/passwd #-i不区分大小写

[root@wwlinux701 ~]# grep -i "root" /root/passwd root:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologinROOT:ROOOOT

grep -n “root” /root/passwd #-n显示行号

[root@wwlinux701 ~]# grep -n "root" /root/passwd 1:root:x:0:0:root:/root:/bin/bash10:operator:x:11:0:operator:/root:/sbin/nologin

grep -v “sbin” /root/passwd #-v取反

[root@wwlinux701 ~]# grep -v "sbin" /root/passwd root:x:0:0:root:/root:/bin/bashww:x:1000:1000::/home/ww:/bin/bashROOT:ROOOOTROOOOOT:3123151roooot:#45345toooos:2423

这里写图片描述

-r遍历所有子目录
-A后面跟数字。过滤出符合要求的行以及下面n行
-B后面跟数字,过滤出符合要求的行以及上面n行
-C后面跟数字,同时过滤出符合要求的行以及上下各n行

grep -n ‘root’ /root/passwd #过滤包含root的行并显示行号
grep -nv ‘nologin’ /root/passwd #过滤不包含nologin的行并显示行号
grep ‘[0-9]’ /root/inittab #查找包含数字的行

[root@wwlinux701 ~]# grep  '[0-9]' /root/inittab# multi-user.target: analogous to runlevel 3# graphical.target: analogous to runlevel 5

grep -v ‘[0-9]’ /root/inittab #过滤不包含数字的行

[root@wwlinux701 ~]# grep -v '[0-9]' /root/inittab# inittab is no longer used when using systemd.## ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.

grep -v ‘^#’ /root/inittab #过滤不包含#的行,^#不是#开头的行

[root@wwlinux701 ~]# grep -v '^#' /root/inittab Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.targetiewr#35466

grep -v ‘^#’ /root/inittab|grep -v ‘^'  #先过滤出不是#开头的行,再在中间过滤出不是卡头的行(第二个grep中不能用特殊符号 ^$ 表示空行,以空开头 以空结尾 就是空行)

[root@wwlinux701 ~]# grep -v '^#' /root/inittab | grep -v '^3' Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target$# multi-user.target: analogous to runlevel 3iewr#$12312[root@wwlinux701 ~]# grep -v '^#' /root/inittab | grep -v '^i' Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target$# multi-user.target: analogous to runlevel 335466$12312

这里写图片描述

grep ‘^[^a-zA-Z]’ /root/passwd #查出不含字母的含
[root@wwlinux701 ~]# grep ‘^[^a-zA-Z]’ /root/passwd
234234235
&&

grep ‘m.s’ /root/passwd #“.”表示任意一个字符,查找m开头+中间任意一个字符+s结尾

[root@wwlinux701 ~]# grep 'm.s' /root/passwdgames:x:12:100:games:/usr/games:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinmysql:x:1017:1017::/home/mysql:/sbin/mologin

grep ‘oo*’ /root/passwd # “”表示0个或多个前面这个字符
grep ‘o*o’ /root/passwd #*左边的这个字符重复0到n次,一次是‘oo’,两次是‘ooo’
这里写图片描述
grep ‘.*’ /root/passwd #匹配所有字符串
grep ‘o{2}’ /root/passwd #匹配o出现2次 等于 grep -E ‘o{2}’ /root/passwd

[root@wwlinux701 ~]# grep 'o\{2\}' /root/passwdroot:x:0:0:root:/root:/bin/bashlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinroooot:#45345toooos:2423[root@wwlinux701 ~]# grep 'o\{3\}' /root/passwdroooot:#45345toooos:2423

这里写图片描述
egrep可以不脱意{},grep -E也可以达到egrep效果
egrep ‘o{2}’ /root/passwd #效果同grep ‘o{2}’ /root/passwd
egrep ‘o+t’ /root/passwd # +这个符号前面的重复一次或者n次,*号是0次或者多次
这里写图片描述
egrep ‘oo?’ /root/passwd #?前面这个字符重复次数为0或者1
egrep ‘root|nologin’ /root/passwd #包含root或者nologin的行
egrep ‘(oo){2}’ /root/passwd #连续两个00在一起的行

[root@wwlinux701 ~]# egrep '(oo){2}' /root/passwdroooot:#45345toooos:2423

这里写图片描述

扩展
把一个目录下,过滤所有*.php文档中含有eval的行
grep -r –include=”*.php” ‘eval’ /data/

原创粉丝点击