正则表达式

19 篇文章 0 订阅

5.使用正则表达式
问题
本案例要求熟悉正则表达式的编写,完成以下任务:
利用egrep工具练习正则表达式的基本用法
提取出httpd.conf文件的有效配置行
编写正则表达式,分别匹配MAC地址、E-Mail邮箱地址、IP地址、主机名
方案
步骤
实现此案例需要按照如下步骤进行。
步骤一:正则表达式匹配练习
1)典型的应用场合:grep、egrep检索文本行
使用不带-E选项的grep命令时,支持基本正则匹配模式。比如“word”关键词检索、“^word”匹配以word开头的行、“wordKaTeX parse error: Expected 'EOF', got '#' at position 47: …: [root@svr5 ~]#̲ grep '^r' /etc…’ /etc/hosts
127.0.0.1 localhost.localdomain localhost
若希望在grep检索式同时组合多个条件,比如输出以“root”或者以“daemon”开头的行,这时候基本正则就不太方便了(“或者”必须转义为“|”):
[root@svr5 ~]# grep ‘root|daemon’ /etc/passwd //搜索无结果
[root@svr5 ~]#
[root@svr5 ~]# grep ‘root|daemon’ /etc/passwd //正确获得结果
root❌0:0:root:/root:/bin/bash
daemon❌2:2:daemon:/sbin:/sbin/nologin
而若若使用grep -E或egrep命令,可支持扩展正则匹配模式,能够自动识别 |、{ 等正则表达式中的特殊字符,用起来更加方便,比如:
[root@svr5 ~]# grep -E ‘root|daemon’ /etc/passwd
root❌0:0:root:/root:/bin/bash
daemon❌2:2:daemon:/sbin:/sbin/nologin
或者
[root@svr5 ~]# egrep ‘root|daemon’ /etc/passwd
root❌0:0:root:/root:/bin/bash
daemon❌2:2:daemon:/sbin:/sbin/nologin
使用grep -E 与 使用egrep命令完全等效,推荐使用后者,特别是涉及到复杂的正则表达式的时候。
2)grep、egrep命令的-q选项
选项 -q 表示 quiet(静默)的意思,结合此选项可以只做检索而并不输出,通常在脚本内用来识别查找的目标是否存在,通过返回状态 KaTeX parse error: Expected 'EOF', got '#' at position 110: …: [root@svr5 ~]#̲ grep '^192.168… —— 匹配行首、行尾
输出默认运行级别的配置记录(以id开头的行):
[root@svr5 ~]# egrep ‘^id’ /etc/inittab
id:3:initdefault:
输出主机名配置记录(以HOSTNAME开头的行):
[root@svr5 ~]# egrep ‘^HOSTNAME’ /etc/sysconfig/network
HOSTNAME=svr5.tarena.com
统计本地用户中登录Shell为“/sbin/nologin”的用户个数:
[root@svr5 ~]# egrep -m10 ‘/sbin/nologinKaTeX parse error: Expected 'EOF', got '#' at position 468: …n [root@svr5 ~]#̲ egrep -c '/sbi…’ /etc/passwd
32 //结合 -c 选项输出匹配的行数
使用 -c 选项可输出匹配行数,这与通过管道再 wc -l的效果是相同的,但是写法更简便。比如,统计使用“/bin/bash”作为登录Shell的正常用户个数,可执行:
[root@svr5 ~]# egrep -c ‘/bin/bashKaTeX parse error: Expected 'EOF', got '#' at position 34: …者 [root@svr5 ~]#̲ egrep '/bin/ba…’ /etc/passwd | wc -l
26
4)基本元字符 . —— 匹配任意单个字符
以/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
输出/etc/rc.local文件内至少包括一个字符(\n换行符除外)的行,即非空行:
[root@svr5 ~]# egrep ‘.’ /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
输出/etc/rc.local文件内的空行(用 –v 选项将条件取反):
[root@svr5 ~]# egrep -v ‘.’ /etc/rc.local

[root@svr5 ~]#
上述取空行的操作与下列操作效果相同:
[root@svr5 ~]# egrep ‘^$’ /etc/rc.local

[root@svr5 ~]#
5)基本元字符 +、?、* —— 目标出现的次数
还以/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
输出包括 f、ff、ff、……的行,即“f”至少出现一次:
[root@svr5 ~]# egrep ‘f+’ /etc/rc.local

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.

输出包括init、initial的行,即末尾的“ial”最多出现一次(可能没有):
[root@svr5 ~]# egrep ‘init(ial)?’ /etc/rc.local

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.

输出包括stu、stuf、stuff、stufff、……的行,即末尾的“f”可出现任意多次,也可以没有。重复目标只有一个字符时,可以不使用括号:
[root@svr5 ~]# egrep ‘stuf*’ /etc/rc.local

You can put your own initialization stuff in here if you don’t

want to do the full Sys V style init stuff.

输出所有行,单独的“.”可匹配任意行(包括空行):
[root@svr5 ~]# egrep '.
’ /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
输出/etc/passwd文件内“r”开头且以“nologin”结尾的用户记录,即中间可以是任意字符:
[root@svr5 ~]# egrep ‘^r.*nologin$’ /etc/passwd
rpc❌32:32:Portmapper RPC user:/:/sbin/nologin
rpcuser❌29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
6)元字符 {} —— 限定出现的次数范围
创建一个练习用的测试文件:
[root@svr5 ~]# vim brace.txt
ab def ghi abdr
dedef abab ghighi
abcab CD-ROM
TARENA IT GROUP
cdcd ababab
Hello abababab World
输出包括ababab的行,即“ab”连续出现3次:
[root@svr5 ~]# egrep ‘(ab){3}’ brace.txt
cdcd ababab
Hello abababab World
输出包括abab、ababab、abababab的行,即“ab”连续出现2~4次:
[root@svr5 ~]# egrep ‘(ab){2,4}’ brace.txt
dedef abab ghighi
cdcd ababab
Hello abababab World
输出包括ababab、abababab、……的行,即“ab”最少连续出现3次:
[root@svr5 ~]# egrep ‘(ab){3,}’ brace.txt
cdcd ababab
Hello abababab World
7)元字符 [] —— 匹配范围内的单个字符
还以前面的测试文件bracet.txt为例:
[root@svr5 ~]# cat brace.txt
ab def ghi abdr
dedef abab ghighi
abcab CD-ROM
TARENA IT GROUP
cdcd ababab
Hello abababab World
输出包括abc、abd的行,即前两个字符为“ab”,第三个字符只要是c、d中的一个就符合条件:
[root@svr5 ~]# egrep ‘ab[cd]’ brace.txt
ab def ghi abdr
abcab CD-ROM
输出包括大写字母的行,使用[A-Z]匹配连续范围:
[root@svr5 ~]# egrep ‘[A-Z]’ brace.txt
abcab CD-ROM
TARENA IT GROUP
Hello abababab World
输出包括“非空格也非小写字母”的其他字符的行,本例中大写字母和 – 符合要求:
[root@svr5 ~]# egrep ‘[^ a-zA-Z]’ brace.txt
abcab CD-ROM
8)单词边界匹配
以文件/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® PRO/1000 Network Driver - version 7.3.21-k4-3-NAPI
e1000: eth0: e1000_probe: Intel® 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 "^KaTeX parse error: Expected 'EOF', got '#' at position 134: …: [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

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:838 errors:0 dropped:0 overruns:0 frame:0
TX packets:838 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:93855 (91.6 KiB) TX bytes:93855 (91.6 KiB)
[root@svr5 ~]# ifconfig | egrep ‘<[0-9]{1,3}(.[0-9]{1,3}){3}>’
inet addr:192.168.4.4 Bcast:192.168.4.255 Mask:255.255.255.0
inet addr:127.0.0.1 Mask:255.0.0.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值