国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > regex > 正文

linux下的通配符與正則表達(dá)式

2020-01-20 22:01:59
字體:
供稿:網(wǎng)友

通配符

  *  任意字符,可重復(fù)多次
    ? 任意字符,重復(fù)一次
    [] 代表一個(gè)字符

舉例: [a,b,c] 表示abc中任意一個(gè)

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

正則表達(dá)式

正則表達(dá)式是在文件中匹配符合條件的字符串的

ls find cp是不支持正則表達(dá)式的

但是grep awk sed支持正則表達(dá)式

[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

正則表達(dá)式特殊字符

正則表達(dá)式匹配范圍

正則表達(dá)式標(biāo)準(zhǔn)字符

使用正則表達(dá)式

 grep "1" /etc/passwd

包含關(guān)鍵字1的行,grep只要包含就行,不想通配符,要完全一致

[root@hadoop-bigdata01 test]# grep "1" /etc/passwdbin:x:1:1:bin:/bin:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologinavahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinwang:x:501:501::/home/wang:/bin/bashgrep 'root' /etc/passwdcat /etc/passwd | grep 'root'

都是同樣的道理,但是管道符更吃資源

所以

1.匹配含有數(shù)字的行    

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

2.匹配連續(xù)含有三個(gè)數(shù)字的行

 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/passwdgames:x:12:100:games:/usr/games:/sbin/nologinusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologinrtkit:x:499:497:RealtimeKit:/proc:/sbin/nologinavahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologinsaslauth:x:498:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologinpulse:x:497:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologinliucheng:x:500:500::/home/liucheng:/bin/bashwang:x:501:501::/home/wang:/bin/bas

3.匹配以r開頭 n結(jié)尾的行

 grep '^r.*n$' /etc/passwd.*代表所有[root@hadoop-bigdata01 test]# grep '^r.*n$' /etc/passwd        rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologinrtkit:x:499:497:RealtimeKit:/proc:/sbin/nologinrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

4.過濾ifconfig ,截取ip

grep -v 代表反向截取,意思就是去除帶有某關(guān)鍵字的行    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

誤區(qū)

這里有個(gè)誤區(qū),想了好久,是正則表達(dá)式和通配符的區(qū)別

我們知道通配符的*指的是任意字符,可重復(fù)多次 正則表達(dá)式的*指的是匹配前一個(gè)字符>=0次

這兩個(gè)是完全不同的,那如何知道我用的*是通配符還是正則表達(dá)式

起初我陷入一個(gè)誤區(qū),看下面這串命令

[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'aacabbcabcac[root@hadoop-bigdata01 test]# ls | grep 'a.*c'aacabbcabcac[root@hadoop-bigdata01 test]# ls | grep '^a.*c'aacabbcabcac[root@hadoop-bigdata01 test]# ls | grep '^a*c' aacac

為什么grep 'a*c'  和 grep '^a*c$' 的結(jié)果會(huì)不一樣,我以為一個(gè)是通配符,一個(gè)是正則,因?yàn)閍*c顯示的四個(gè)結(jié)果,正好

不就是匹配任意多個(gè)字符嗎?

其實(shí)不然

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

正則表達(dá)式是在文件中匹配符合條件的字符串的

交給管道符之后使用grep已經(jīng)不是匹配文件名了,這是對(duì)文件的操作,所以說,他完全就是正則表達(dá)式

grep 'a*c' 表示的是匹配a>=0個(gè)所以只要含有c就是可以的

而grep '^a*c$'也是正則,表示的是以a開頭,且第二個(gè)字符匹配a零次或者多次,接下來是c字母的

所以只有aac 和ac 符合條件

所以看這個(gè)例子

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

這里grep 'a*b' 指的可不是含有a和b 而是a重復(fù)0次或者多次然后含有b

以上所述是小編給大家介紹的linux下的通配符與正則表達(dá)式,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大邑县| 淮安市| 华亭县| 江油市| 伊川县| 文水县| 大关县| 奉节县| 余庆县| 许昌县| 栖霞市| 连城县| 类乌齐县| 怀仁县| 宝鸡市| 修武县| 通道| 龙井市| 漠河县| 罗田县| 和硕县| 五莲县| 美姑县| 东乌| 五大连池市| 漯河市| 仙居县| 于都县| 喀喇| 栖霞市| 囊谦县| 富宁县| 六枝特区| 美姑县| 抚顺市| 富源县| 彭山县| 葫芦岛市| 张家港市| 云浮市| 丰都县|