正则表达式是一种符号表示法,用于识别文本模式。php
元字符mysql
正则表达式的元字符包括如下字符正则表达式
^ $ . [] {} - ? * + () | \sql
“.” 点。匹配任意单个字符shell
“^”和“$” 锚。匹配行的开头和结尾。例如:grep -i '^..j.r$' /词典
用于填字游戏express
“[ ]” 方括号。匹配括号内的一个字符。括号内的^表示否认,“-”表示范围,bash
export LANG=POSIX
“|” 或。echo "AAA" | grep AAA|BBB|CCC
输出AAA工具
“?”、“*”、“+”、“{}” 限定符。分别表示匹配0或1次、0或屡次、1或屡次、匹配次数.net
全称:global regular expression print 全局正则表达式打印(工具)code
语法:grep [options] regex [file...]
例如:grep -i 'dog' txt //中的dog即便正则表达式,不过经过特殊符号能够让表达式匹配更多含义
选项:功能描述
grep/egrp示例
eg.1 基本用法
[root@ax-01 ~]# grep -n 'root' /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin [root@ax-01 ~]# grep -nv 'nologin' !$ grep -nv 'nologin' /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 6:sync:x:5:0:sync:/sbin:/bin/sync 7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8:halt:x:7:0:halt:/sbin:/sbin/halt 25:mysql:x:1000:1000::/home/mysql:/bin/bash 26:user1:x:1001:1001::/home/user1:/bin/bash [root@ax-01 ~]# grep '[0-9]' /etc/inittab # multi-user.target: analogous to runlevel 3 # graphical.target: analogous to runlevel 5
eg.2 特殊符号
[root@ax-01 ~]# cat txt xxx Math English C++ Experiment Monkey 100 90 95 Good Cat 80 100 60 Perfect Dog 90 60 70 Great Tiger 95 85 90 Fantastic @http://blog.csdn.net/stpeace/article/details/46848873 # awk基本用法简介 2015-07-12 19:49 by stpeace [root@ax-01 ~]# grep '^[^a-zA-Z]' txt @http://blog.csdn.net/stpeace/article/details/46848873 # awk基本用法简介 2015-07-12 19:49 by stpeace [root@ax-01 ~]# grep '.o' txt Monkey 100 90 95 Good Dog 90 60 70 Great @http://blog.csdn.net/stpeace/article/details/46848873 [root@ax-01 ~]# grep 'ot*' txt Monkey 100 90 95 Good Dog 90 60 70 Great
eg.3 有些特殊符号须要加-E或者用egrep来实现grep预想的效果
[root@ax-01 ~]# grep -E 'o{2}' txt Monkey 100 90 95 Good [root@ax-01 ~]# egrep 'o{2}' txt Monkey 100 90 95 Good [root@ax-01 ~]# egrep 'o+' txt Monkey 100 90 95 Good Dog 90 60 70 Great @http://blog.csdn.net/stpeace/article/details/46848873
eg.4 连带显示后面-A,前面-B 先后-C
[root@lixiang01 grep]# grep -nA2 'root' passwd 1:root:x:0:0:root:/root:/bin/bash 2-bin:x:1:1:bin:/bin:/sbin/nOloGin 3-daemon:x:2:2:daemon:/sbin:/sbin/nologin -- 10:operator:x:11:0:operator:/root:/sbin/nologin 11-games:x:12:100:games:/usr/games:/sbin/nologin 12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
eg.5 不区分大小写-i
[root@lixiang01 grep]# grep -inA2 'root' passwd 1:root:x:0:0:root:/root:/bin/bash 2-bin:x:1:1:bin:/bin:/sbin/nOloGin 3-daemon:x:2:2:daemon:/sbin:/sbin/nologin -- 10:operator:x:11:0:operator:/RooT:/sbin/nologin 11-games:x:12:100:games:/usr/games:/sbin/nologin 12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
grep -n '[a-zA-Z]' inittab #输出含字母的行 grep -nv '[a-zA-Z]' inittab #输出不含字母的行 grep -n '^[a-zA-Z]' inittab #输出字母为首的行 grep -n '^[^a-zA-Z]' inittab #输出非字母为首的行 grep -nv '^[^a-zA-Z]' inittab #输出字母为首的行 grep -nv '[^a-zA-Z]' inittab #输出不含非字母的行 grep -nv '^[a-zA-Z]' inittab #输出非字母为首的行
[root@lixiang01 grep]# cat -n 111.txt 1 r rr rrr rrrr 2 o oo ooo oooo 3 ro or rr oo 4 rrr 5 rro 6 roo 7 ror 8 orr 9 oor 10 ooo 11 rrrr 12 rrro 13 rroo 14 rooo 15 orrr 16 oorr 17 ooor 18 oooo 19 roro 20 oror 21 roor 22 orro 23 rorroorrrooorrrroooorrrrrooooo
[root@lixiang01 grep]# grep -n 'r.o' 111
[root@lixiang01 grep]# grep -n 'oo*' 111
[root@lixiang01 grep]# grep -n '.*' 111
[root@lixiang01 grep]# grep -n 'o\{2\}' 111 [root@lixiang01 grep]# egrep -n 'o{2}' 111
[root@lixiang01 grep]# egrep -n 'oor+' 111 [root@lixiang01 grep]# egrep -n 'oor?' 111
[root@lixiang01 grep]# egrep -n 'roo|ror|orr' 111
[root@lixiang01 grep]# egrep '(oo){2}' 111
扩展: 把一个目录下,过滤全部*.php文档中含有eval的行 grep -r --include="*.php" 'eval' /data/
[root@ax-01 ~]# grep -r --include="*txt" 'Dog' /root/ /root/txt:Dog 90 60 70 Great /root/test.txt:Dog [root@ax-01 ~]# grep 'Dog' /root/*txt /root/test.txt:Dog /root/txt:Dog 90 60 70 Great