源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-04-09 08:27 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:linux下的通配符与正则表达式
[b]通配符[/b]   *  任意字符,可重复多次     ? 任意字符,重复一次     [] 代表一个字符 举例: [a,b,c] 表示abc中任意一个 通配符的作用是用来匹配文件名的 [b]正则表达式[/b] 正则表达式是在文件中匹配符合条件的字符串的 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 正则表达式特殊字符 [img]http://files.jb51.net/file_images/article/201705/201705230841461.png[/img] 正则表达式匹配范围 [img]http://files.jb51.net/file_images/article/201705/201705230841462.png[/img] 正则表达式标准字符 [img]http://files.jb51.net/file_images/article/201705/201705230841463.png[/img] 使用正则表达式
 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
[b]3.匹配以r开头 n结尾的行[/b]
 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
[b]4.过滤ifconfig ,截取ip[/b] 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
[b]误区[/b] 这里有个误区,想了好久,是正则表达式和通配符的区别 我们知道通配符的*指的是任意字符,可重复多次 正则表达式的*指的是匹配前一个字符>=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下的通配符与正则表达式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程素材网网站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部