加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux 正则表达式_010

发布时间:2020-12-14 02:05:24 所属栏目:Linux 来源:网络整理
导读:? ? Linux 正则表达式 ? ? 标注: 本教程只针对linux运维的 三剑客命令awk,sed,grep 正则表达式 ? ? 什么是正则表达式? 简单的说,正则表达式就是为处理大量的字符串而定义的一套规则和方法通过定义的这些特殊符号的辅助, 系统管理员就可以快速过滤,替

?

?

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、注意使用单引号和双引号,建议使用双引号

?

?

?

普通正则表达式

字符

功能

举例

^

^w匹配以w开头的内容

范例1

$

w$匹配以w结尾的内容

范例1

^$

显示空行

范例1

.

只能代表任意一个字符

范例1

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

范例1

*

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

范例1

. *

匹配所有

范例2

^.*

以任意多个字符开头

范例2

.*$

以任意多个字符结尾

范例2

[abc]

匹配字符集合内的是一个字符[a-zA-Z]

范例2

[^abc]

匹配不包含^后的任意一个字符的内容

范例2

a{n,m}

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

范例3

a{n,}

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

范例3

a{n}

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

范例3

a{,m}

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

范例3

?

?

扩展正则表达式

字符

功能

举例

|

扩展正则表达式 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为例子讲解)

?

[[email?protected] ~]# 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:

[[email?protected] ~]# grep "^my" file.txt

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

my qq num is 1300052.

my god,but OLDBOY!

[[email?protected] ~]# grep "com$" file.txt

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

[[email?protected] ~]# grep -n "^$" file.txt

3:

8:

[[email?protected] ~]# grep "13...52" file.txt

my qq num is 1300052.

[[email?protected] ~]# grep "//" file.txt

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

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

[[email?protected] ~]# grep "//" file.txt

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

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

[[email?protected] ~]# grep ‘//‘ file.txt

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

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

[[email?protected] ~]# grep "130*52" file.txt

my qq num is 1300052.

not 130000052.

?

?

范例2:

[[email?protected] ~]# grep ".*" file.txt

I am oldboy teacher!

I teach linux.

?

I like badminton ball,but OLDBOY!

[[email?protected] ~]# grep "^my.*" file.txt

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

my qq num is 1300052.

my god,but OLDBOY!

[[email?protected] ~]# grep "org.*$" file.txt

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

[[email?protected] ~]# grep "[0-9]" file.txt

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

my qq num is 1300052.

not 130000052.

[[email?protected] ~]# 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,but OLDBOY!

?

?

范例3:

[[email?protected] ~]# grep -E "0{4,5}" file.txt

not 130000052.

[[email?protected] ~]# grep -E "0{4,}" file.txt

not 130000052.

[[email?protected] ~]# grep -E "0{3}" file.txt

my qq num is 1300052.

not 130000052.

[[email?protected] ~]# grep -E "0{,4}" file.txt

I am oldboy teacher!

I teach linux.

?

I like badminton ball,but OLDBOY!

?

?

范例4:

[[email?protected] ~]# echo 2oldboy >> file.txt

[[email?protected] ~]# echo 3oldboy4 >> file.txt

[[email?protected] ~]# grep "oldboy" file.txt

I am oldboy teacher!

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

my god,but OLDBOY!

2oldboy

3oldboy4

[[email?protected] ~]# grep "boldboy" file.txt

I am oldboy teacher!

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

my god,but OLDBOY!

[[email?protected] ~]# grep "boldboyb" file.txt

I am oldboy teacher!

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

my god,but OLDBOY!

?

?

范例5:

[[email?protected] ~]# 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

[[email?protected] ~]# 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

?

[[email?protected] ~]# 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)

[[email?protected] ~]# ifconfig eth3 | sed -n ‘2p‘|sed ‘s#^.*dr:##g‘|sed ‘s#? B.*$##g‘

10.8.9.65

[[email?protected] ~]# ifconfig eth3 | sed -n ‘2s#^.*dr:##gp‘ | sed ‘s#? B.*$##g‘

10.8.9.65

?

?

2、sed后向引用

sed -n ?‘s#()()#12#gp‘? file

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

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

[[email?protected] ~]# 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)

[[email?protected] ~]# ifconfig eth3 | sed -nr ‘s#^.*dr:(.*)? Bc.*$#1#gp‘

10.8.9.65

[[email?protected] ~]# stat /etc/hosts | sed -nr ‘s#^.*ess: ((.*)/-.*$#1#gp‘

0644

[[email?protected] ~]# sed -nr ‘s#([^:]+)(:.*:)(/.*$)#321#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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读