正则表达式 四

单词边界匹配

以文件/etc/rc.local为例:

[root@svr5 ~]# cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

输出包括单词“init”的行,文件中“initialization”不合要求:

[root@svr5 ~]# egrep '\binit\b' /etc/rc.local
# This script will be executed *after* all the other init scripts.
# want to do the full Sys V style init stuff.

或者:

[root@svr5 ~]# egrep '\<init\>' /etc/rc.local
# This script will be executed *after* all the other init scripts.
# want to do the full Sys V style init stuff.

输出包括以“ll”结尾的单词的行,使用 > 匹配单词右边界:

[root@svr5 ~]# egrep 'll\>' /etc/rc.local
# This script will be executed *after* all the other init scripts.
# want to do the full Sys V style init stuff.

或者:

[root@svr5 ~]# egrep 'll\b' /etc/rc.local
# This script will be executed *after* all the other init scripts.
# want to do the full Sys V style init stuff.

9)多个条件的组合
通过dmesg启动日志查看与IDE接口、CDROM光盘相关的设备信息:

[root@svr5 ~]# egrep '\<IDE\>|\<CDROM\>' /var/log/dmesg
Uniform Multi-Platform E-IDE driver Revision: 7.00alpha2
PIIX4: IDE controller at PCI slot 0000:00:07.1
Probing IDE interface ide0...
Probing IDE interface ide1...
hdc: VMware Virtual IDE CDROM Drive, ATAPI CD/DVD-ROM drive
Probing IDE interface ide0...

通过dmesg启动日志查看蓝牙设备、网卡设备相关的信息:

[root@svr5 ~]# egrep -i 'eth|network|bluetooth' /var/log/dmesg
Initalizing network drop monitor service
Bluetooth: Core ver 2.10
Bluetooth: HCI device and connection manager initialized
Bluetooth: HCI socket layer initialized
Bluetooth: HCI USB driver ver 2.9
Intel(R) PRO/1000 Network Driver - version 7.3.21-k4-3-NAPI
e1000: eth0: e1000_probe: Intel(R) PRO/1000 Network Connection

步骤二:利用正则表达式完成检索任务

1)提取出httpd.conf文件的有效配置行
以RHEL6自带的httpd软件包为例,默认的httpd.conf配置文件内提供了大量的注释信息(# 开头或空几个格再 #),以及一些分隔的空行:

[root@svr5 ~]# head  /etc/httpd/conf/httpd.conf  //确认文件内容
#
# This is the main Apache server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See <URL:http://httpd.apache.org/docs/2.2/> for detailed information.
# In particular, see
# <URL:http://httpd.apache.org/docs/2.2/mod/directives.html>
# for a discussion of each configuration directive.
#
#
# Do NOT simply read the instructions in here without understanding
[root@svr5 ~]# egrep -c ".*" /etc/httpd/conf/httpd.conf
991  											//总行数
[root@svr5 ~]# egrep -c "#" /etc/httpd/conf/httpd.conf
674  											//含注释的行数
[root@svr5 ~]# egrep -c "^$" /etc/httpd/conf/httpd.conf
95  												//空行的数量

提取有效配置行,也就是说应排除掉注释行、空行,根据上面的结果可得知有效配置行的数量应该是“991-674-95=222”,确认一下:

[root@svr5 ~]# egrep -c -v '#|^$'  /etc/httpd/conf/httpd.conf
222

结合 > 重定向操作,提取httpd.conf的有效配置,将其保存到文件 httpd.conf.min,相关操作如下:

[root@svr5 ~]# egrep -v '#|^$'  /etc/httpd/conf/httpd.conf > httpd.conf.min
[root@svr5 ~]# head httpd.conf.min  		//确认有效配置的前10行
ServerTokens OS
ServerRoot "/etc/httpd"
PidFile run/httpd.pid
Timeout 120
KeepAlive Off
MaxKeepAliveRequests 100
KeepAliveTimeout 15
<IfModule prefork.c>
StartServers       8
MinSpareServers    5

2)匹配MAC地址、邮箱地址、IP地址
以使用ifconfig查看的结果为例,MAC地址的特征是以“:”分隔的6组十六进制数,每组由2个字符组成,比如:

[root@svr5 ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:82:09:E9
          inet addr:192.168.4.4  Bcast:192.168.4.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe82:9e9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:177666 errors:0 dropped:0 overruns:0 frame:0
          TX packets:101720 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:18454015 (17.5 MiB)  TX bytes:13467792 (12.8 MiB)

采用正则表达式匹配“00:0C:29:82:09:E9”形式的MAC地址,可以写成:

[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}

或者:
[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}
其中,[0-9a-fA-F]{2} 表示一段十六进制数值,第二种方式的{5}表示连续出现5组前置 : 的十六进制。
因此,若要从ifconfig eth0的输出结果中过滤出包含MAC地址值的行,可以执行以下操作:

[root@svr5 ~]# ifconfig eth0 | egrep '[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}'
eth0      Link encap:Ethernet  HWaddr 00:0C:29:82:09:E9

或者:

[root@svr5 ~]# ifconfig eth0 | egrep \
'[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}'
eth0      Link encap:Ethernet  HWaddr 00:0C:29:82:09:E9

根据上述操作结果,稍微扩展一下。利用MAC匹配条件,可以检查一个变量的值是否是合法的MAC地址。参考下列操作:
先定义三个变量:

[root@svr5 ~]# MAC01="00:50:56:C0:00:08"
[root@svr5 ~]# MAC02="20:68:9D:48:C4:98"
[root@svr5 ~]# MAC03="20:69:74:R2:C5:27"  			//设一个无效地址

利用正则表达式判断出其中哪个MAC地址是无效的:

[root@svr5 ~]# echo $MAC01 | egrep -q \
'[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}' && echo "有效" || echo "无效"
有效
[root@svr5 ~]# echo $MAC02 | egrep -q \
'[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}' && echo "有效" || echo "无效"
有效
[root@svr5 ~]# echo $MAC03 | egrep -q \
'[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}' && echo "有效" || echo "无效"
无效
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值