正则表达式练习题:
1、统计/etc/目录下有多少个链接文件
显示/etc目录下以cnf结尾的文件
2、去掉注释行 显示httpd.conf文件中的有效行
grep -E -v '^#|^ ' httpd.conf
不显示文件的空行和注释行
grep -E -v '^#|^ |^$' httpd.conf
3、不显示文件中有数字的行
grep -v [0-9] xs.txt
不显示文件中以数字开头的行
grep -v ^[0-9] xs.txt
只显示文件中以字母开头的行
grep ^[a-Z] xs.txt
显示文件中以3个字母结尾的行
grep -E [a-Z]{3}$ xs.txt
显示文件中以1个数字结尾的行
grep -E [0-9]{1}$ xs.txt
显示文件中有tarena字样的行
grep 'tarena' xs.txt
显示文件有plj或tarena字样的行
grep -E 'plj|tarena' xs.txt
4、显示以空格开头的行
grep '^ ' xs.txt
5、显示文件中有空格的行
grep '^$' xs.txt
显示文件中以3个空格开头的行
grep '^ ' xs.txt
6、去掉空行显示文件内容
grep -v '^$' httpd.conf
7、显示由plj三个字母组成的行
grep plj xs.txt
8、显示有符号 ? 或 - 或 _ 的行
grep [?_-] xs.txt
9、匹配密码的正则表达式
密码总长度为8位 只能由数字或字母组成,但必须以数字开头
grep -E ^[0-9][0-9a-Z]{7}$ xs.txt
10、显示网站服务 访问日志文件中的URL路径
<http://主机名/目录名/文件名.html
.css
.php
http;//pc1.wsyht.com/index.html
http://pc1.wsyht.com/p_w_picpath/a.html
http://localhost/muisc/song.mp3
http://www.wsyht.com.cn/phpmyadmin/index.php
11、匹配MAC地址的正则表达式
grep -E '([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}' ifcfg-eth0
12、匹配主机名的正则表达式 .org .net .com .cn .edu .gov .hk
pc1.wsyht.com.cn
www.baidu.com
mail.126.com
13、匹配邮箱地址的正则表达式
@163.com
@yahoo.net
@sina.com.cn x.x.x.x
123@ www.baidu.com.cn 255
suen11@ 63
pplj_20@
14、匹配ip地址的正则表达式
x.x.x.x 1.1.1.1
xx.xx.xx.xx 20.0.100.1
xxx.xxx.xxx.xxx 215.10.0.199
0-255
一位数 0-9
[0-9]
二位数 10-99
[1-9][0-9]
三位数 100-255
100-299 1[0-9][0-9]
200-249 2[0-4][0-9]
250-255 25[0-5]
转载于:https://blog.51cto.com/yht1990/1790499