通配符

  *  任意字符,可重复多次

    ? 任意字符,重复一次

    [] 代表一个字符

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

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

正则表达式

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

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

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

linux通配符与正则表达式_linux

[root@hadoop-bigdata01 test]# touch aa[root@hadoop-bigdata01 test]# touch aab aabb[root@hadoop-bigdata01 test]# lltotal 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 aaaa
[root@hadoop-bigdata01 test]# ls aa?aab
[root@hadoop-bigdata01 test]# ls aa*aa  aab  aabb

linux通配符与正则表达式_linux

正则表达式特殊字符

 linux通配符与正则表达式_linux_03

正则表达式匹配范围


linux通配符与正则表达式_通配符_04

正则表达式标准字符

linux通配符与正则表达式_linux_05

使用正则表达式

 grep "1" /etc/passwd 

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

linux通配符与正则表达式_linux

[root@hadoop-bigdata01 test]# grep "1" /etc/passwdbin: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

linux通配符与正则表达式_linux

grep 'root' /etc/passwd

cat /etc/passwd | grep 'root'

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

所以

1.匹配含有数字的行    

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

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

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

linux通配符与正则表达式_linux

[root@hadoop-bigdata01 test]# grep '[0-9][0-9][0-9]'  /etc/passwdgames: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

linux通配符与正则表达式_linux

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

 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有替换的意思

linux通配符与正则表达式_linux

[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

linux通配符与正则表达式_linux

误区

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

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

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

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

linux通配符与正则表达式_linux

[root@hadoop-bigdata01 test]# touch ac aac abc abbc[root@hadoop-bigdata01 test]# lltotal 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

linux通配符与正则表达式_linux

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

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

其实不然

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

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

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

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

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

所以只有aac 和ac 符合条件

所以看这个例子

linux通配符与正则表达式_linux

[root@hadoop-bigdata01 test]# lsa  aac  abb  abbc  abc  ac  b  bb  c  cb
[root@hadoop-bigdata01 test]# ls | grep 'a*b'abb
abbc
abc
b
bb
cb

linux通配符与正则表达式_linux

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