Linux 正则表达式_010

 

 

Linux 正则表达式

 

 

标注:本教程只针对linux运维的三剑客命令awk,sed,grep正则表达式

 

 

什么是正则表达式?

  简单的说,正则表达式就是为处理大量的字符串而定义的一套规则和方法通过定义的这些特殊符号的辅助,

系统管理员就可以快速过滤,替换或输出需要的字符串,linux正则表达式一般以行为单位处理

 

 

为什么要学会正则表达式?

  在企业工作中,我们每天做的linux运维工作中, 时刻都会面对大量带有字符串的文本配置、程序、命令输出及日志文件等,

而我们经常会有迫切的需要,从大量的字符串内容中查找符合工作需要的特定字符串。这就要靠正则表达式。因此,可以说正则

表达式就是为过滤这样字符串的需求而生Llinux最常应用的正则表达式的命令 grep(egrep)/sed/awk,换名话说linux三剑客要想能

工作的更高效,那一定离不开正则表达式配合。

 

 

正则表达式注意事项

1、linux正则表达式一般以行为单位处理的

2、设置别名alias grep='grep --color=auto',让匹配的内容显示颜色

3、注意字符集,使用LC_ALL=C,在/etc/profile配置文件最后添加一行export LC_ALL=C

4、注意使用单引号和双引号,建议使用双引号

 

 

 

普通正则表达式BRE

字符

功能

举例

^

^w匹配以w开头的内容

范例1

$

w$匹配以w结尾的内容

范例1

^$

显示空行

范例1

.

只能代表任意一个字符

范例1

\

转义字符,例如\.仅代表点 .

范例1

*

重复0个或多个前面的一个字符,例如 w*匹配没有w或1个w或多个w

范例1

. *

匹配所有

范例2

[abc]

匹配括号内的任意一个字符,其它写法[a-zA-Z]

范例2

[^abc]

不匹配括号内的任意一个字符,其它写法[a-zA-Z]

范例2

a\{n,m\}

重复n到m次,前一个重复的字符

范例3

a\{n,\}

重复最少n次,前一个重复的字符

范例3

a\{n\}

重复n次,前一个重复字符

范例3

a\{,m\}

重复最多m次,前一个重复字符

范例3

 

 

 

扩展正则表达式ERE

字符

功能

举例

|

扩展正则表达式 egrep 或 grep –E,取多个不同的匹配字符

范例5

a{n,m}

重复n到m次,前一个重复的字符

范例3

a{n,}

重复最少n次,前一个重复的字符

范例3

a{n}

重复n次,前一个重复字符

范例3

a{,m}

重复最多m次,前一个重复字符

范例3

\b

元字符,单词边界

范例4

+

匹配前面一个字符1次或1次以上

 

?

匹配前一个字符0次或1次

 

 

 

 

 

举例(以grep为例子讲解)

 

[root@oldboy ~]# cat file.txt

I am oldboy teacher!

I teach linux.

 

I like badminton ball,billiard ball and chinese chess!

my blog is http://oldboy.blog.51cto.com

our site is http://www.etiantian.org

my qq num is 1300052.

 

not 130000052.

my god,i am not oldboy,but OLDBOY!

 

 

范例1:

[root@oldboy ~]# grep "^my" file.txt

my blog is http://oldboy.blog.51cto.com

my qq num is 1300052.

my god,i am not oldboy,but OLDBOY!

[root@oldboy ~]# grep "com$" file.txt

my blog is http://oldboy.blog.51cto.com

[root@oldboy ~]# grep -n "^$" file.txt

3:

8:

[root@oldboy ~]# grep "13...52" file.txt

my qq num is 1300052.

[root@oldboy ~]# grep "//" file.txt

my blog is http://oldboy.blog.51cto.com

our site is http://www.etiantian.org

[root@oldboy ~]# grep "\/\/" file.txt

my blog is http://oldboy.blog.51cto.com

our site is http://www.etiantian.org

[root@oldboy ~]# grep '//' file.txt

my blog is http://oldboy.blog.51cto.com

our site is http://www.etiantian.org

[root@oldboy ~]# grep "130*52" file.txt

my qq num is 1300052.

not 130000052.

 

 

范例2:

[root@oldboy ~]# grep ".*" file.txt

I am oldboy teacher!

I teach linux.

 

I like badminton ball,billiard ball and chinese chess!

my blog is http://oldboy.blog.51cto.com

our site is http://www.etiantian.org

my qq num is 1300052.

 

not 130000052.

my god,i am not oldboy,but OLDBOY!

[root@oldboy ~]# grep "^my.*" file.txt

my blog is http://oldboy.blog.51cto.com

my qq num is 1300052.

my god,i am not oldboy,but OLDBOY!

[root@oldboy ~]# grep "org.*$" file.txt

our site is http://www.etiantian.org

[root@oldboy ~]# grep "[0-9]" file.txt

my blog is http://oldboy.blog.51cto.com

my qq num is 1300052.

not 130000052.

[root@oldboy ~]# grep "[^a-z]" file.txt

I am oldboy teacher!

I teach linux.

I like badminton ball,billiard ball and chinese chess!

my blog is http://oldboy.blog.51cto.com

our site is http://www.etiantian.org

my qq num is 1300052.

not 130000052.

my god,i am not oldboy,but OLDBOY!

 

 

范例3:

[root@oldboy ~]# grep -E "0{4,5}" file.txt

not 130000052.

[root@oldboy ~]# grep -E "0{4,}" file.txt

not 130000052.

[root@oldboy ~]# grep -E "0{3}" file.txt

my qq num is 1300052.

not 130000052.

[root@oldboy ~]# grep -E "0{,4}" file.txt

I am oldboy teacher!

I teach linux.

 

I like badminton ball,billiard ball and chinese chess!

my blog is http://oldboy.blog.51cto.com

our site is http://www.etiantian.org

my qq num is 1300052.

 

not 130000052.

my god,i am not oldboy,but OLDBOY!

 

 

范例4:

[root@oldboy ~]# echo 2oldboy >> file.txt

[root@oldboy ~]# echo 3oldboy4 >> file.txt

[root@oldboy ~]# grep "oldboy" file.txt

I am oldboy teacher!

my blog is http://oldboy.blog.51cto.com

my god,i am not oldboy,but OLDBOY!

2oldboy

3oldboy4

[root@oldboy ~]# grep "\boldboy" file.txt

I am oldboy teacher!

my blog is http://oldboy.blog.51cto.com

my god,i am not oldboy,but OLDBOY!

[root@oldboy ~]# grep "\boldboy\b" file.txt

I am oldboy teacher!

my blog is http://oldboy.blog.51cto.com

my god,i am not oldboy,but OLDBOY!

 

 

范例5:

[root@oldboy ~]# grep -E "[0-9]|qq|linux" file.txt

I teach linux.

my blog is http://oldboy.blog.51cto.com

my qq num is 1300052.

not 130000052.

2oldboy

3oldboy4

[root@oldboy ~]# egrep "[0-9]|qq|linux" file.txt

I teach linux.

my blog is http://oldboy.blog.51cto.com

my qq num is 1300052.

not 130000052.

2oldboy

3oldboy4

 

 

 

 

Linux正则表达式结合三剑客企业级实践

 

1、取系统IP地址

替换功能

sed ‘s#支持正则位置##g’   file

sed  -n ‘s#支持正则位置##gp’  file

 

[root@oldboy ~]# ifconfig eth3

eth3      Link encap:Ethernet  HWaddr 00:0C:29:A7:4E:51 

          inet addr:10.8.9.65  Bcast:10.8.9.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fea7:4e51/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:134218 errors:0 dropped:0 overruns:0 frame:0

          TX packets:31049 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:13502212 (12.8 MiB)  TX bytes:2379801 (2.2 MiB)

[root@oldboy ~]# ifconfig eth3 | sed -n '2p'|sed 's#^.*dr:##g'|sed 's#  B.*$##g'

10.8.9.65

[root@oldboy ~]# ifconfig eth3 | sed -n '2s#^.*dr:##gp' | sed 's#  B.*$##g'

10.8.9.65

 

 

2、sed后向引用

sed -n  's#()()#\1\2#gp'  file

当在前面匹配部分用小括号的时候,第一个括号内容,可以在后面的部分用\1输出。

第二个括号内容,可以在后面的部分用\2输出

[root@oldboy ~]# ifconfig eth3

eth3      Link encap:Ethernet  HWaddr 00:0C:29:A7:4E:51 

          inet addr:10.8.9.65  Bcast:10.8.9.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fea7:4e51/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:134828 errors:0 dropped:0 overruns:0 frame:0

          TX packets:31546 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:13565103 (12.9 MiB)  TX bytes:2431777 (2.3 MiB)

[root@oldboy ~]# ifconfig eth3 | sed -nr 's#^.*dr:(.*)  Bc.*$#\1#gp'

10.8.9.65

[root@oldboy ~]# stat /etc/hosts | sed -nr 's#^.*ess: \((.*)\/-.*$#\1#gp'

0644

[root@oldboy ~]# sed -nr 's#([^:]+)(:.*:)(/.*$)#\3\2\1#gp' /etc/passwd | head -4

/bin/bash:x:0:0:root:/root:root

/sbin/nologin:x:1:1:bin:/bin:bin

/sbin/nologin:x:2:2:daemon:/sbin:daemon

/sbin/nologin:x:3:4:adm:/var/adm:adm

 

posted @ 2019-03-06 17:19  邹龙彬  阅读(385)  评论(0编辑  收藏  举报