標籤:blog alt 反向 iuc 關鍵字 user linu test 誤區
萬用字元 * 任一字元,可重複多次 ? 任一字元,重複一次
[] 代表一個字元舉例: [a,b,c] 表示abc中任意一個萬用字元的作用是用來匹配檔案名稱的RegexRegex是在檔案中匹配合格字串的ls find cp是不支援Regex的但是grep awk sed支援Regex
[[email protected] test]# touch aa[[email protected] test]# touch aab aabb[[email protected] 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[[email protected]-bigdata01 test]# ls aaaa[[email protected]-bigdata01 test]# ls aa?aab[[email protected]-bigdata01 test]# ls aa*aa aab aabb
Regex特殊字元 Regex匹配範圍
Regex標準字元
使用Regex grep "1" /etc/passwd 包含關鍵字1的行,grep只要包含就行,不想萬用字元,要完全一致
[[email protected] 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/bash
grep ‘root‘ /etc/passwd
cat /etc/passwd | grep ‘root‘
都是同樣的道理,但是管道符更吃資源所以1.匹配含有數位行 grep ‘[0-9]‘ /etc/passwd2.匹配連續含有三個數位行 grep ‘[0-9][0-9][0-9]‘ /etc/passwd 或者 grep ‘:[0-9][0-9][0-9]:‘ /etc/passwd
[[email protected] 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結尾的行 grep ‘^r.*n$‘ /etc/passwd
.*代表所有
[[email protected] 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 ,截取ipgrep -v 代表反向截取,意思就是去除帶有某關鍵字的行 sed有替換的意思
[[email protected] 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[[email protected]-bigdata01 test]# [[email protected] 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[[email protected]-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[[email protected]-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
誤區這裡有個誤區,想了好久,是Regex和萬用字元的區別我們知道萬用字元的*指的是任一字元,可重複多次 Regex的*指的是匹配前一個字元>=0次這兩個是完全不同的,那如何知道我用的*是萬用字元還是Regex起初我陷入一個誤區,看下面這串命令
[[email protected] test]# touch ac aac abc abbc[[email protected] 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[[email protected]-bigdata01 test]# ls | grep ‘a*c‘aacabbcabcac[[email protected]-bigdata01 test]# ls | grep ‘a.*c‘aacabbcabcac[[email protected]-bigdata01 test]# ls | grep ‘^a.*c‘aacabbcabcac[[email protected]-bigdata01 test]# ls | grep ‘^a*c‘ aacac
為什麼grep ‘a*c‘ 和 grep ‘^a*c$‘ 的結果會不一樣,我以為一個是萬用字元,一個是正則,因為a*c顯示的四個結果,正好不就是匹配任意多個字元嗎?其實不然萬用字元的作用是用來匹配檔案名稱的Regex是在檔案中匹配合格字串的交給管道符之後使用grep已經不是匹配檔案名稱了,這是對檔案的操作,所以說,他完全就是Regexgrep ‘a*c‘ 表示的是匹配a>=0個所以只要含有c就是可以的而grep ‘^a*c$‘也是正則,表示的是以a開頭,且第二個字元匹配a零次或者多次,接下來是c字母的所以只有aac 和ac 符合條件所以看這個例子
[[email protected] test]# lsa aac abb abbc abc ac b bb c cb[[email protected]-bigdata01 test]# ls | grep ‘a*b‘abbabbcabcbbbcb
這裡grep ‘a*b‘ 指的可不是含有a和b 而是a重複0次或者多次然後含有b
linux萬用字元與Regex