详解Linux中正则表达式的应用(2)

[root@localhost ~]# grep -E "3306|1521" /etc/services
mysql           3306/tcp                        # MySQL
mysql           3306/udp                        # MySQL
ncube-lm        1521/tcp                # nCube License Manager
ncube-lm        1521/udp                # nCube License Manager
[root@localhost ~]#

4.4、( ) :分组过滤,后向引用

分组过滤   

[root@localhost ~]# echo "glad" >> test.txt
[root@localhost ~]# egrep "(la|oo)" test.txt
good
goood
glad

()后向引用;当前面匹配部分用小括号的时候,第一个括号的内容可以在后面部分用\1输出;以此类推。

[root@localhost tmp]# ifconfig |sed -rn 's#.*addr:(.*)(B.*)$#\1#gp'
192.168.4.27 

5、正则表达式的元字符

5.1、\b :匹配一个单词边界

[root@localhost tmp]# cat test       
do
does
doxy
agdoeg
[root@localhost tmp]# grep "do\b" test
do
[root@localhost tmp]# grep "\bdo" test       
do
does
doxy
[root@localhost tmp]# grep "\bdoes" test         
does
[root@localhost tmp]# grep "\bdo\b" test 
do
[root@localhost tmp]#

5.2、\B :匹配非单词边界,与\b相反

[root@localhost tmp]# grep "do\B" test   
does
doxy
agdoeg
[root@localhost tmp]# grep "do\b" test
do
[root@localhost tmp]#

5.3、\d :匹配一个数字字符,等价于[0-9]

5.4、\D :匹配一个非数字字符,等价于[^0-9]

5.5、\w :匹配字母、数字、下划线,等价于[A-Za-z0-9_]

还有很多元字符,这里就不一一罗列出来

案例:开机精简

[root@localhost ~]# chkconfig --list| egrep -v "crond|network|rsyslog|sshd|sysstat" | awk '{print "chkconfig",$1,"off"}'|bash

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjswzj.html