正则表达式
正则表达式 一、什么是正则表达式? 正则表达式是用“一串符号”描述有共同属性的数据。 那么正则表达式能用作什么呢? 其实正则表达式就是一种工具,和其他工具一样,它是为了解决某一类专门的问题而发明的。从根本上讲,它有两种基本的用途,搜索和替换。 正则表达式并不是一种完备的程序设计语言,它甚至算不上一种能够直接安装并运行的程序。更准确的说,正则表达式语言是内置于其他语言或软件产品里的“迷你”语言。好在现在几乎所有的语言或工具都支持正则表达式。正则表达式有它自己的语法规则与用法。 二、正则表达式的基本用法 1正则表达式怎么写呢? 格式一: grep 【选项】 ‘正则表达式’ 文件列表 处理的数据从文件列表中来 格式二、 命令 |gerp 【选项】 ‘正则表达式’ 处理数据从命令中来 元字符组成正则表达式,用来匹配有共同属性的数据, 选项用来改变命令的处理方式,是可选项,不写时按默认方式处理 2常用的元字符: ^ : 匹配行首:^a 匹配以字母a开头的行(^ab 显示以a开头,后面紧跟是b的行) $ :匹配行尾(a$ 匹配以字母a结尾的行) .$ 匹配除换行符号n之外的任意单个字符结尾的行 如果想让“.”成为一个字符输出,加转意符号 ^$ :匹配空行 *匹配前边的正则表达式出现的次数( 次数>=0 ) 3常用的选项有: -v :取反 (输出与正则表达式不匹配的行) --color :标红显示与正则表达式匹配的数据(默认输出一行) -n :显示与正则表达式匹配的行在文件中行号 -c :显示与正则表达式匹配的行数 -q :不输出与正则表达式匹配的行(可用于判断要找的字段是否) -i :忽略大小写匹配 ( ) :把数据作为整体来匹配,使用时需要用转义符号 -E :扩展匹配,有转义时直接用E 去掉转义符号 4正则表达式还可以进行范围内匹配 范围内匹配: 【awf】匹配字符a或者w或者f 【091】匹配数字0或者9或者1 【a-z】 匹配所有的小写字母 【A-Z】大写 【a-Z】所有大小写 【0-9】匹配所有的数字 【a-Z0-9】数字和字母 【^a-z】^放到范围内匹配时表示范围内取反,匹配不是小写字母的 【^0-9】不是字母 ^【a-z0-9】 匹配以范围内开头 *************注意-有连续的意思,当做字符的时候别放到中间 5正则表达式出现次数匹配 *前面的正则表达式出现0次到多出 {n,m}匹配前边的正则表达式出现指定的次数 {5}匹配前边的正则表达式必须出现5次 {5,}匹配前边的正则表达式次数大于等于5次 {5,8}匹配前边的正则表达式最少出现5次,最多出现8次 grep ?E ‘g(ooo){3}d’a.txt 三次 grep ?E ‘g(ooo){3,}d’a.txt 最少三次 grep ?E ‘g(ooo){3,5}d’a.txt 三次到五次 + 匹配前边的正则表达式出现1次到N次 ? 匹配前边的正则表达式出现0次到1次 * 匹配前边的正则表达式出现0次到N次 grep ?E ‘go+d’a.txt 一次到多次 grep ?E ‘go?d’a.txt 0次到一次 6查找单词 &; 匹配单词的开头 grep ?color “&;th” b.txt 以th开头的行 &; 匹配单词的开头 三、看的好枯燥,下面举例看一下正则表达式的应用: 1、典型的应用场合:grep、egrep检索文本行 使用不带-E选项的grep命令时,支持基本正则匹配模式。比如“word”关键词检索、“^word”匹配以word开头的行、“word$”匹配以word结尾的行……等等。 输出以“r”开头的用户记录: # grep '^r' /etc/passwd root:x:0:0:root:/root:/bin/bash rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin 输出以“localhost”结尾的行: 1.# grep 'localhost$' /etc/hosts 2.127.0.0.1 localhost.localdomain localhost 若希望在grep检索式同时组合多个条件,比如输出以“root”或者以“daemon”开头的行,这时候基本正则就不太方便了(“或者”必须转义为“|”): 1.# grep '^root|^daemon' /etc/passwd //搜索无结果 # grep '^root|^daemon' /etc/passwd //正确获得结果 4.root:x:0:0:root:/root:/bin/bash 5.daemon:x:2:2:daemon:/sbin:/sbin/nologin 而若若使用grep -E或egrep命令,可支持扩展正则匹配模式,能够自动识别 |、{ 等正则表达式中的特殊字符,用起来更加方便,比如: 1.# grep -E '^root|^daemon' /etc/passwd 2.root:x:0:0:root:/root:/bin/bash 3.daemon:x:2:2:daemon:/sbin:/sbin/nologin 或者 # egrep '^root|^daemon' /etc/passwd 使用grep -E 与 使用egrep命令完全等效,推荐使用后者,特别是涉及到复杂的正则表达式的时候;个人也比较喜欢用后者,没有扩展的时候扩展一下,也不会出错。 2)grep、egrep命令的-q选项 选项 -q 表示 quiet(静默)的意思,结合此选项可以只做检索而并不输出,通常在脚本内用来识别查找的目标是否存在,通过返回状态 $? 来判断,这样可以忽略无关的文本信息,简化脚本输出。 比如,检查/etc/hosts文件内是否存在192.168.4.4的映射记录,如果存在则显示“YES”,否则输出“NO”,一般会执行: # grep '^192.168.4.4' /etc/hosts && echo "YES" || echo "NO" 2.192.168.4.4 svr5.tarena.com svr5 3.YES 这样grep的输出信息和脚本判断后的提示混杂在一起,用户不易辨别,所以可以改成以下操作: # grep -q '^192.168.4.4' /etc/hosts && echo "YES" || echo "NO" 2.YES 是不是清爽多了,从上述结果也可以看到,使用 -q 选项的效果与使用 &> /dev/null的效果类似。 3)基本元字符 ^、$ ―― 匹配行首、行尾 输出默认运行级别的配置记录(以id开头的行): # egrep '^id' /etc/inittab 2.id:3:initdefault: 统计本地用户中登录Shell为“/sbin/nologin”的用户个数: # egrep -m10 '/sbin/nologin$' /etc/passwd //先确认匹配正确 2.bin:x:1:1:bin:/bin:/sbin/nologin 3.daemon:x:2:2:daemon:/sbin:/sbin/nologin 4.adm:x:3:4:adm:/var/adm:/sbin/nologin 5.lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6.mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 7.uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin 8.operator:x:11:0:operator:/root:/sbin/nologin 9.games:x:12:100:games:/usr/games:/sbin/nologin 10.gopher:x:13:30:gopher:/var/gopher:/sbin/nologin 11.ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 12.[root@svr5 ~]# egrep -c '/sbin/nologin$' /etc/passwd 13.32 //结合 -c 选项输出匹配的行数 使用 -c 选项可输出匹配行数,这与通过管道再 wc -l的效果是相同的,但是写法更简便。比如,统计使用“/bin/bash”作为登录Shell的正常用户个数,可执行: # egrep -c '/bin/bash$' /etc/passwd 26 或者 # egrep '/bin/bash$' /etc/passwd | wc -l 26 4)基本元字符 . ―― 匹配任意单个字符 以/etc/rc.local文件为例,确认文本内容: 1.# cat /etc/rc.local 2.#!/bin/sh 3.# 4.# This script will be executed *after* all the other init scripts. 5.# You can put your own initialization stuff in here if you don't 6.# want to do the full Sys V style init stuff. 7. 8.touch /var/lock/subsys/local 输出/etc/rc.local文件内至少包括一个字符(n换行符除外)的行,即非空行: 1.# egrep '.' /etc/rc.local 2.#!/bin/sh 3.# 4.# This script will be executed *after* all the other init scripts. 5.# You can put your own initialization stuff in here if you don't 6.# want to do the full Sys V style init stuff. 7.touch /var/lock/subsys/local 输出/etc/rc.local文件内的空行(用 ?v 选项将条件取反): .# egrep -v '.' /etc/rc.local 上述取空行的操作与下列操作效果相同: # egrep '^$' /etc/rc.local 5)基本元字符 +、?、* ―― 目标出现的次数 还以/etc/rc.local文件为例: 1. # cat /etc/rc.local 2.#!/bin/sh 3.# 4.# This script will be executed *after* all the other init scripts. 5.# You can put your own initialization stuff in here if you don't 6.# want to do the full Sys V style init stuff. 7. 8.touch /var/lock/subsys/local 输出包括 f、ff、ff、……的行,即“f”至少出现一次: 1. # egrep 'f+' /etc/rc.local 2.# This script will be executed *after* all the other init scripts. 3.# You can put your own initialization stuff in here if you don't 4.# want to do the full Sys V style init stuff. 输出包括init、initial的行,即末尾的“ial”最多出现一次(可能没有): 1.# egrep 'init(ial)?' /etc/rc.local 2.# This script will be executed *after* all the other init scripts. 3.# You can put your own initialization stuff in here if you don't 4.# want to do the full Sys V style init stuff. 输出包括stu、stuf、stuff、stufff、……的行,即末尾的“f”可出现任意多次,也可以没有。重复目标只有一个字符时,可以不使用括号: 1.# egrep 'stuf*' /etc/rc.local 2.# You can put your own initialization stuff in here if you don't 3.# want to do the full Sys V style init stuff. 输出所有行,单独的“.*”可匹配任意行(包括空行): 1.]# egrep '.*' /etc/rc.local 2.#!/bin/sh 3.# 4.# This script will be executed *after* all the other init scripts. 5.# You can put your own initialization stuff in here if you don't 6.# want to do the full Sys V style init stuff. 7. 8.touch /var/lock/subsys/local 输出/etc/passwd文件内“r”开头且以“nologin”结尾的用户记录,即中间可以是任意字符: 1. # egrep '^r.*nologin$' /etc/passwd 2.rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin 3.rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin 6)元字符 {} ―― 限定出现的次数范围 创建一个练习用的测试文件: 1. # vim brace.txt 2.ab def ghi abdr 3.dedef abab ghighi 4.abcab CD-ROM 5.TARENA IT GROUP 6.cdcd ababab 7.Hello abababab World 输出包括ababab的行,即“ab”连续出现3次: 1.otegrep '(ab){3}' brace.txt 2.cdcd ababab 3.Hello abababab World 输出包括abab、ababab、abababab的行,即“ab”连续出现2~4次: 1.]# egrep '(ab){2,4}' brace.txt 2.dedef abab ghighi 3.cdcd ababab 4.Hello abababab World 输出包括ababab、abababab、……的行,即“ab”最少连续出现3次: 1.# egrep '(ab){3,}' brace.txt 2.cdcd ababab 3.Hello abababab World 7)元字符 [] ―― 匹配范围内的单个字符 还以前面的测试文件bracet.txt为例: 1.#cat brace.txt 2.ab def ghi abdr 3.dedef abab ghighi 4.abcab CD-ROM 5.TARENA IT GROUP 6.cdcd ababab 7.Hello abababab World 输出包括abc、abd的行,即前两个字符为“ab”,第三个字符只要是c、d中的一个就符合条件: 1.# egrep 'ab[cd]' brace.txt 2.ab def ghi abdr 3.abcab CD-ROM 输出包括大写字母的行,使用[A-Z]匹配连续范围: 1.[root@svr5 ~]# egrep '[A-Z]' brace.txt 2.abcab CD-ROM 3.TARENA IT GROUP 4.Hello abababab World 输出包括“非空格也非小写字母”的其他字符的行,本例中大写字母和 ? 符合要求: 1.# egrep '[^ a-zA-Z]' brace.txt 2.abcab CD-ROM 8)单词边界匹配 以文件/etc/rc.local为例: 1.# cat /etc/rc.local 2.#!/bin/sh 3.# 4.# This script will be executed *after* all the other init scripts. 5.# You can put your own initialization stuff in here if you don't 6.# want to do the full Sys V style init stuff. 7. 8.touch /var/lock/subsys/local 输出包括单词“init”的行,文件中“initialization”不合要求: 1.#egrep 'binitb' /etc/rc.local 2.# This script will be executed *after* all the other init scripts. 3.# want to do the full Sys V style init stuff. 或者: 1.# egrep '&;init&;' /etc/rc.local 2.# This script will be executed *after* all the other init scripts. 3.# want to do the full Sys V style init stuff. 输出包括以“init”开头的单词的行,使用 &; 匹配单词左边界: 1.# egrep '&;init' /etc/rc.local 2.# This script will be executed *after* all the other init scripts. 3.# You can put your own initialization stuff in here if you don't 4.# want to do the full Sys V style init stuff. 或者: 1.# egrep 'binit' /etc/rc.local 2.# This script will be executed *after* all the other init scripts. 3.# You can put your own initialization stuff in here if you don't 4.# want to do the full Sys V style init stuff. 输出包括以“ll”结尾的单词的行,使用 &; 匹配单词右边界: 1.# egrep 'll&;' /etc/rc.local 2.# This script will be executed *after* all the other init scripts. 3.# want to do the full Sys V style init stuff. 或者: 1.#egrep 'llb' /etc/rc.local 2.# This script will be executed *after* all the other init scripts. 3.# want to do the full Sys V style init stuff. 9)多个条件的组合 通过dmesg启动日志查看与IDE接口、CDROM光盘相关的设备信息: 1.]# egrep '&;IDE&;|&;CDROM&;' /var/log/dmesg 2.Uniform Multi-Platform E-IDE driver Revision: 7.00alpha2 3.PIIX4: IDE controller at PCI slot 0000:00:07.1 4.Probing IDE interface ide0... 5.Probing IDE interface ide1... 6.hdc: VMware Virtual IDE CDROM Drive,ATAPI CD/DVD-ROM drive 7.Probing IDE interface ide0... 通过dmesg启动日志查看蓝牙设备、网卡设备相关的信息: 1.#egrep -i 'eth|network|bluetooth' /var/log/dmesg 2.Initalizing network drop monitor service 3.Bluetooth: Core ver 2.10 4.Bluetooth: HCI device and connection manager initialized 5.Bluetooth: HCI socket layer initialized 6.Bluetooth: HCI USB driver ver 2.9 7.Intel(R) PRO/1000 Network Driver - version 7.3.21-k4-3-NAPI 8.e1000: eth0: e1000_probe: Intel(R) PRO/1000 Network Connection (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |