Linux-正则表达式_Linux

正则表达式

正则表达式REGEXPRegular Expression 用来匹配字符()

REGEXP:由一类特殊字符及文本字符所编写的模式,其中有些字符(元字符)不表示字符字面意义,而表示控制或通配的功能

程序支持:grep,sed,awk,vim, less,nginx,varnish

分两类:

基本正则表达式:BRE

扩展正则表达式:ERE

grep -E, egrep

正则表达式引擎:

采用不同算法,检查处理正则表达式的软件模块

PCREPerl Compatible Regular Expressions

元字符分类:字符匹配、匹配次数、位置锚定、分组

man 7 regex

 

基本正则表达式元字符

字符匹配:

. 匹配任意单个字符

[] 匹配指定范围内的任意单个字符

[^] 匹配指定范围外的任意单个字符

[:alnum:] 字母和数字

[:alpha:] 代表任何英文大小写字符,亦即 A-Z, a-z

[:lower:] 小写字母 [:upper:] 大写字母

[:blank:] 空白字符(空格和制表符)

[:space:] 水平和垂直的空白字符(比[:blank:]包含的范围广)

[:cntrl:] 不可打印的控制字符(退格、删除、警铃...

[:digit:] 十进制数字 [:xdigit:]十六进制数字

[:graph:] 可打印的非空白字符

[:print:] 可打印字符

[:punct:] 标点符号

 

转义\,如.表示任意单个字符,\.则表示.本身

.如果放在[]里边,就表示.本身,不需要转义

 

匹配次数:用在要指定次数的字符后面,用于指定前面的字符要出现的次数

* 匹配前面的字符任意次,包括0

贪婪模式:尽可能长的匹配

.* 任意长度的任意字符

\? 匹配其前面的字符01

\+ 匹配其前面的字符至少1

\{n\} 匹配前面的字符n

\{m,n\} 匹配前面的字符至少m次,至多n

\{,n\} 匹配前面的字符至多n

\{n,\} 匹配前面的字符至少n

 

位置锚定:定位出现的位置

^ 行首锚定,用于模式的最左侧

$ 行尾锚定,用于模式的最右侧

^PATTERN$ 用于模式匹配整行

^$ 空行

^[[:space:]]*$ 空白行

\< \b 词首锚定,用于单词模式的左侧

\> \b 词尾锚定;用于单词模式的右侧

\<PATTERN\> 匹配整个单词

 

grep命令摘出ifconfig ens33命令里的ip地址:

[root@centos7 ~]#ifconfig ens33 | grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" | head -n1

[root@centos7 ~]#ifconfig ens33 | grep -o "inet [0-9.]\+" | cut -d " " -f2

……

 

过滤空行

[root@centos7 data]#grep -v "^[[:space:]]*$" f1

 

分组:\(\) 将一个或多个字符捆绑在一起,当作一个整体进行处理,如:\(root\)\+

分组括号中的模式匹配到的内容会被正则表达式引擎记录于内部的变量中,这些变量的命名方式为: \1, \2, \3, ...

\1 表示从左侧起第一个左括号以及与之匹配右括号之间的模式所匹配到的字符

示例: \(string1\+\(string2\)*\)

\1 string1\+\(string2\)*

\2 string2

后向引用:引用前面的分组括号中的模式所匹配字符,而非模式本身

或者:\|

示例:a\|b: ab C\|cat: Ccat \(C\|c\)at:Catcat

 

查看系统大版本号

[root@centos7 ~]#grep -o "[0-9]\+" /etc/redhat-release | head -n1

[root@centos7 ~]#grep -o " [0-9]\+" /etc/redhat-release | grep -o "[0-9]\+"

 

windows中的正则表达式:cmd>findstr ?

 

练习

1、显示/proc/meminfo文件中以大小s开头的行(要求:使用两种方法)

[root@centos7 ~]#grep -i "^s" /proc/meminfo

[root@centos7 ~]#grep "^[Ss].*" /proc/meminfo

2、显示/etc/passwd文件中不以/bin/bash结尾的行

[root@centos7 ~]#cat /etc/passwd | grep -v "/bin/bash$"

3、显示用户rpc默认的shell程序

[root@centos7 ~]#cat /etc/passwd | grep "^rpc\>" | cut -d: -f7

/sbin/nologin

4、找出/etc/passwd中的两位或三位数

[root@centos7 ~]#cat /etc/passwd | grep -wo "[[:digit:]]\{2,3\}"

5、显示CentOS7/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非空白字符的行

[root@centos7 ~]#cat /etc/grub2.cfg | grep "^[[:space:]].*[^[:space:]].*"

6、找出“netstat -tan”命令结果中以LISTEN后跟任意多个空白字符结尾的行

[root@centos7 data]#netstat -tan | grep "LISTEN[[:space:]]*$"

7、显示CentOS7上所有系统用户的用户名和UID

[root@centos7 data]#cat /etc/passwd | cut -d: -f1,3 | grep  ":[1-9][0-9]\{,2\}$"

8、添加用户bashtestbashbashershnologin(shell/sbin/nologin),找出/etc/passwd用户名和shell同名的行

[root@centos7 ~]#cat /etc/passwd | grep "^\(.\+\):.*/\1$"

[root@centos7 ~]#cat /etc/passwd | grep "^\(.*\):.*/\1$"

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

bash:x:1001:1001::/home/bash:/bin/bash

nologin:x:1005:1005::/home/nologin:/sbin/nologin

9、利用dfgrep,取出磁盘各分区利用率,并从大到小排序

[root@centos7 ~]#df | grep "/dev/sd" | grep -o "[0-9]*%" | sort -nr

15%

8%

1%

 

egrep及扩展的正则表达式

egrep = grep -E

egrep [OPTIONS] PATTERN [FILE...]

扩展正则表达式的元字符:

字符匹配:

. 任意单个字符

[] 指定范围的字符

[^] 不在指定范围的字符

 

扩展正则表达式

次数匹配:

*:匹配前面字符任意次

?: 01

+1次或多次

{m}:匹配m

{m,n}:至少m,至多n

 

位置锚定:

^ :行首

$ :行尾

\<, \b :语首

\>, \b :语尾

分组:

()

后向引用:\1, \2, ...

或者:

a|b: ab

C|cat: Ccat

(C|c)at:Catcat

 

练习

1、显示三个用户rootmageqjyUID和默认shell

[root@centos7 ~]#cat /etc/passwd | egrep -w "^(root|mage|qjy)" | cut -d: -f1,3,7

2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行

[root@centos7 ~]#cat /etc/rc.d/init.d/functions | grep "^[[:alpha:]_]\+()"

3、使用egrep取出/etc/rc.d/init.d/functions中其基名

[root@centos7 ~]#echo /etc/rc.d/init.d/functions | egrep -o "[^/]*/?$"

4、使用egrep取出上面路径的目录名

echo /etc/rc.d/init.d/functions | egrep -o ".*/[^$]" | egrep -o ".*/"

5、统计last命令中以root登录的每个主机IP地址登录次数

last | egrep root | egrep -wo "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" | sort | uniq -c | sort -nr

6、利用扩展正则表达式分别表示0-910-99100-199200-249250-255

[root@centos7 ~]#egrep -w [0-9]

[root@centos7 ~]#egrep -w [1-9][0-9]

[root@centos7 ~]#egrep -w 1[0-9] [0-9]

[root@centos7 ~]#egrep -w 2[0-4][0-9]

[root@centos7 ~]#egrep -w 25[0-5]

7、显示ifconfig命令结果中所有IPv4地址

[root@centos7 ~]#ifconfig | egrep -wo "(([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"

8、将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面

[root@centos7 ~]#echo "welcome to magedu linux" | egrep -o [a-z' '] | sort | uniq -c | sort -nr

[root@centos7 ~]#echo "welcome to magedu linux" | egrep -o . | sort | uniq -c | sort -nr

 

思考

1 手机号

2 邮箱

3 QQ