linux下的通配符与正则表达式

发布时间:2019-08-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了linux下的通配符与正则表达式脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

通配符

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

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

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

正则表达式

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

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

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

[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

正则表达式特殊字符

linux下的通配符与正则表达式

正则表达式匹配范围

linux下的通配符与正则表达式

正则表达式标准字符

linux下的通配符与正则表达式

使用正则表达式

  grep "1" /etc/passwd

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

 [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.匹配含有数字的行    

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

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

  grep '[0-9][0-9][0-9]' /etc/passwd      或者  grep ':[0-9][0-9][0-9]:' /etc/passwd
 [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结尾的行

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

 [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次

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

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

 [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 符合条件

所以看这个例子

 [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下的通配符与正则表达式所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:div