关于linux下通配符以及正则表达式的详解(图)


本文摘自PHP中文网,作者黄舟,侵删。

正则表达式是在文件中匹配符合条件的字符串的。下面通过本文给大家介绍linux下的通配符与正则表达式,需要的的朋友参考下吧

通配符

 * 任意字符,可重复多次
? 任意字符,重复一次
[] 代表一个字符

举例: [a,b,c] 表示abc中任意一个

通配符的作用是用来匹配文件名的

正则表达式

正则表达式是在文件中匹配符合条件的字符串的

ls find cp是不支持正则表达式的

但是grep awk sed支持正则表达式

1

2

3

4

5

6

7

8

9

10

11

12

13

[root@hadoop-bigdata01 test]# touch aa

[root@hadoop-bigdata01 test]# touch aab aabb

[root@hadoop-bigdata01 test]# ll

total 0

-rw-r--r-- 1 root root 0 May 16 19:47 aa

-rw-r--r-- 1 root root 0 May 16 19:47 aab

-rw-r--r-- 1 root root 0 May 16 19:47 aabb

[root@hadoop-bigdata01 test]# ls aa

aa

[root@hadoop-bigdata01 test]# ls aa?

aab

[root@hadoop-bigdata01 test]# ls aa*

aa  aab  aabb

正则表达式特殊字符

正则表达式匹配范围

正则表达式标准字符

使用正则表达式

1

grep "1" /etc/passwd

包含关键字1的行,grep只要包含就行,不想通配符,要完全一致

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

[root@hadoop-bigdata01 test]# grep "1" /etc/passwd

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

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

dbus:x:81:81:System message bus:/:/sbin/nologin

usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin

avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin

abrt:x:173:173::/etc/abrt:/sbin/nologin

wang:x:501:501::/home/wang:/bin/bash

grep 'root' /etc/passwd

cat /etc/passwd | grep 'root'

都是同样的道理,但是管道符更吃资源

所以

1.匹配含有数字的行

1

grep '[0-9]' /etc/passwd

2.匹配连续含有三个数字的行

1

grep '[0-9][0-9][0-9]' /etc/passwd      或者  grep ':[0-9][0-9][0-9]:' /etc/passwd

1

2

3

4

5

6

7

8

9

10

11

[root@hadoop-bigdata01 test]# grep '[0-9][0-9][0-9]' /etc/passwd

games:x:12:100:games:/usr/games:/sbin/nologin

usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin

rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin

avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin

abrt:x:173:173::/etc/abrt:/sbin/nologin

nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

saslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin

pulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin

liucheng:x:500:500::/home/liucheng:/bin/bash

wang:x:501:501::/home/wang:/bin/bas

3.匹配以r开头 n结尾的行

1

2

3

4

5

6

grep '^r.*n$' /etc/passwd

.*代表所有

[root@hadoop-bigdata01 test]# grep '^r.*n$' /etc/passwd       

rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin

rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin

rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

4.过滤ifconfig ,截取ip

grep -v 代表反向截取,意思就是去除带有某关键字的行 sed有替换的意思

1

2

3

4

5

6

7

8

9

10

[root@hadoop-bigdata01 test]# ifconfig | grep 'inet addr:'

     inet addr:192.168.126.191 Bcast:192.168.126.255 Mask:255.255.255.0

     inet addr:127.0.0.1 Mask:255.0.0.0

[root@hadoop-bigdata01 test]#

[root@hadoop-bigdata01 test]# ifconfig | grep 'inet addr:' | grep -v '127.0.0.1'

     inet addr:192.168.126.191 Bcast:192.168.126.255 Mask:255.255.255.0

[root@hadoop-bigdata01 test]# ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | sed 's/inet addr://g'

     192.168.126.191 Bcast:192.168.126.255 Mask:255.255.255.0

[root@hadoop-bigdata01 test]# ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | sed 's/inet addr://g' | sed 's/Bcast.*//g'

     192.168.126.191

误区

这里有个误区,想了好久,是正则表达式和通配符的区别

我们知道通配符的*指的是任意字符,可重复多次 正则表达式的*指的是匹配前一个字符>=0次

这两个是完全不同的,那如何知道我用的*是通配符还是正则表达式

起初我陷入一个误区,看下面这串命令

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

[root@hadoop-bigdata01 test]# touch ac aac abc abbc

[root@hadoop-bigdata01 test]# ll

total 0

-rw-r--r-- 1 root root 0 May 16 19:55 aac

-rw-r--r-- 1 root root 0 May 16 19:55 abbc

-rw-r--r-- 1 root root 0 May 16 19:55 abc

-rw-r--r-- 1 root root 0 May 16 19:55 ac

[root@hadoop-bigdata01 test]# ls | grep 'a*c'

aac

abbc

abc

ac

[root@hadoop-bigdata01 test]# ls | grep 'a.*c'

aac

abbc

abc

ac

[root@hadoop-bigdata01 test]# ls | grep '^a.*c'

aac

abbc

abc

ac

[root@hadoop-bigdata01 test]# ls | grep '^a*c'

aac

ac

为什么grep 'a*c' 和 grep '^a*c$' 的结果会不一样,我以为一个是通配符,一个是正则,因为a*c显示的四个结果,正好

不就是匹配任意多个字符吗?

其实不然

通配符的作用是用来匹配文件名的

正则表达式是在文件中匹配符合条件的字符串的

交给管道符之后使用grep已经不是匹配文件名了,这是对文件的操作,所以说,他完全就是正则表达式

grep 'a*c' 表示的是匹配a>=0个所以只要含有c就是可以的

而grep '^a*c$'也是正则,表示的是以a开头,且第二个字符匹配a零次或者多次,接下来是c字母的

所以只有aac 和ac 符合条件

所以看这个例子

1

2

3

4

5

6

7

8

9

[root@hadoop-bigdata01 test]# ls

a aac abb abbc abc ac b bb c cb

[root@hadoop-bigdata01 test]# ls | grep 'a*b'

abb

abbc

abc

b

bb

cb

这里grep 'a*b' 指的可不是含有a和b 而是a重复0次或者多次然后含有b

以上就是关于linux下通配符以及正则表达式的详解(图)的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

Linux安装rpm包时报错header v3 dsa/sha1 signature, key id 1d1e034b: nokey解决办法

Linux修改主机名的方法

Linux系统安装软件失败的总结

如何快速简单的优化快照使用成本

怎么配置Linux的ip地址?

Linux中mysql密码忘了怎么办

Linux如何查看系统负载

操作系统--Linux的详细介绍

Linux 管道命令是什么?

Linux中无法挂载移动硬盘

更多相关阅读请进入《Linux》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...