Linux 正則表達式linux
標註:本教程只針對linux運維的三劍客命令awk,sed,grep正則表達式正則表達式
什麼是正則表達式?bash
簡單的說,正則表達式就是爲處理大量的字符串而定義的一套規則和方法經過定義的這些特殊符號的輔助,運維
系統管理員就能夠快速過濾,替換或輸出須要的字符串,linux正則表達式通常以行爲單位處理linux運維
爲何要學會正則表達式?spa
在企業工做中,咱們天天作的linux運維工做中, 時刻都會面對大量帶有字符串的文本配置、程序、命令輸出及日誌文件等,日誌
而咱們常常會有迫切的須要,從大量的字符串內容中查找符合工做須要的特定字符串。這就要靠正則表達式。所以,能夠說正則blog
表達式就是爲過濾這樣字符串的需求而生Llinux最常應用的正則表達式的命令 grep(egrep)/sed/awk,換名話說linux三劍客要想能教程
工做的更高效,那必定離不開正則表達式配合。ci
正則表達式注意事項
一、linux正則表達式通常以行爲單位處理的
二、設置別名alias grep='grep --color=auto',讓匹配的內容顯示顏色
三、注意字符集,使用LC_ALL=C,在/etc/profile配置文件最後添加一行export LC_ALL=C
四、注意使用單引號和雙引號,建議使用雙引號
普通正則表達式BRE |
||
字符 |
功能 |
舉例 |
^ |
^w匹配以w開頭的內容 |
範例1 |
$ |
w$匹配以w結尾的內容 |
範例1 |
^$ |
顯示空行 |
範例1 |
. |
只能表明任意一個字符 |
範例1 |
\ |
轉義字符,例如\.僅表明點 . |
範例1 |
* |
重複0個或多個前面的一個字符,例如 w*匹配沒有w或1個w或多個w |
範例1 |
. * |
匹配全部 |
範例2 |
[abc] |
匹配括號內的任意一個字符,其它寫法[a-zA-Z] |
範例2 |
[^abc] |
不匹配括號內的任意一個字符,其它寫法[a-zA-Z] |
範例2 |
a\{n,m\} |
重複n到m次,前一個重複的字符 |
範例3 |
a\{n,\} |
重複最少n次,前一個重複的字符 |
範例3 |
a\{n\} |
重複n次,前一個重複字符 |
範例3 |
a\{,m\} |
重複最多m次,前一個重複字符 |
範例3 |
擴展正則表達式ERE |
||
字符 |
功能 |
舉例 |
| |
擴展正則表達式 egrep 或 grep –E,取多個不一樣的匹配字符 |
範例5 |
a{n,m} |
重複n到m次,前一個重複的字符 |
範例3 |
a{n,} |
重複最少n次,前一個重複的字符 |
範例3 |
a{n} |
重複n次,前一個重複字符 |
範例3 |
a{,m} |
重複最多m次,前一個重複字符 |
範例3 |
\b |
元字符,單詞邊界 |
範例4 |
+ |
匹配前面一個字符1次或1次以上 |
|
? |
匹配前一個字符0次或1次 |
|
舉例(以grep爲例子講解)
[root@oldboy ~]# cat file.txt
I am oldboy teacher!
I teach linux.
I like badminton ball,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 1300052.
not 130000052.
my god,i am not oldboy,but OLDBOY!
範例1:
[root@oldboy ~]# grep "^my" file.txt
my blog is http://oldboy.blog.51cto.com
my qq num is 1300052.
my god,i am not oldboy,but OLDBOY!
[root@oldboy ~]# grep "com$" file.txt
my blog is http://oldboy.blog.51cto.com
[root@oldboy ~]# grep -n "^$" file.txt
3:
8:
[root@oldboy ~]# grep "13...52" file.txt
my qq num is 1300052.
[root@oldboy ~]# grep "//" file.txt
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
[root@oldboy ~]# grep "\/\/" file.txt
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
[root@oldboy ~]# grep '//' file.txt
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
[root@oldboy ~]# grep "130*52" file.txt
my qq num is 1300052.
not 130000052.
範例2:
[root@oldboy ~]# grep ".*" file.txt
I am oldboy teacher!
I teach linux.
I like badminton ball,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 1300052.
not 130000052.
my god,i am not oldboy,but OLDBOY!
[root@oldboy ~]# grep "^my.*" file.txt
my blog is http://oldboy.blog.51cto.com
my qq num is 1300052.
my god,i am not oldboy,but OLDBOY!
[root@oldboy ~]# grep "org.*$" file.txt
our site is http://www.etiantian.org
[root@oldboy ~]# grep "[0-9]" file.txt
my blog is http://oldboy.blog.51cto.com
my qq num is 1300052.
not 130000052.
[root@oldboy ~]# grep "[^a-z]" file.txt
I am oldboy teacher!
I teach linux.
I like badminton ball,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 1300052.
not 130000052.
my god,i am not oldboy,but OLDBOY!
範例3:
[root@oldboy ~]# grep -E "0{4,5}" file.txt
not 130000052.
[root@oldboy ~]# grep -E "0{4,}" file.txt
not 130000052.
[root@oldboy ~]# grep -E "0{3}" file.txt
my qq num is 1300052.
not 130000052.
[root@oldboy ~]# grep -E "0{,4}" file.txt
I am oldboy teacher!
I teach linux.
I like badminton ball,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq num is 1300052.
not 130000052.
my god,i am not oldboy,but OLDBOY!
範例4:
[root@oldboy ~]# echo 2oldboy >> file.txt
[root@oldboy ~]# echo 3oldboy4 >> file.txt
[root@oldboy ~]# grep "oldboy" file.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com
my god,i am not oldboy,but OLDBOY!
2oldboy
3oldboy4
[root@oldboy ~]# grep "\boldboy" file.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com
my god,i am not oldboy,but OLDBOY!
[root@oldboy ~]# grep "\boldboy\b" file.txt
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com
my god,i am not oldboy,but OLDBOY!
範例5:
[root@oldboy ~]# grep -E "[0-9]|qq|linux" file.txt
I teach linux.
my blog is http://oldboy.blog.51cto.com
my qq num is 1300052.
not 130000052.
2oldboy
3oldboy4
[root@oldboy ~]# egrep "[0-9]|qq|linux" file.txt
I teach linux.
my blog is http://oldboy.blog.51cto.com
my qq num is 1300052.
not 130000052.
2oldboy
3oldboy4
Linux正則表達式結合三劍客企業級實踐
一、取系統IP地址
替換功能
sed ‘s#支持正則位置##g’ file
sed -n ‘s#支持正則位置##gp’ file
[root@oldboy ~]# ifconfig eth3
eth3 Link encap:Ethernet HWaddr 00:0C:29:A7:4E:51
inet addr:10.8.9.65 Bcast:10.8.9.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fea7:4e51/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:134218 errors:0 dropped:0 overruns:0 frame:0
TX packets:31049 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:13502212 (12.8 MiB) TX bytes:2379801 (2.2 MiB)
[root@oldboy ~]# ifconfig eth3 | sed -n '2p'|sed 's#^.*dr:##g'|sed 's# B.*$##g'
10.8.9.65
[root@oldboy ~]# ifconfig eth3 | sed -n '2s#^.*dr:##gp' | sed 's# B.*$##g'
10.8.9.65
二、sed後向引用
sed -n 's#()()#\1\2#gp' file
當在前面匹配部分用小括號的時候,第一個括號內容,能夠在後面的部分用\1輸出。
第二個括號內容,能夠在後面的部分用\2輸出
[root@oldboy ~]# ifconfig eth3
eth3 Link encap:Ethernet HWaddr 00:0C:29:A7:4E:51
inet addr:10.8.9.65 Bcast:10.8.9.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fea7:4e51/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:134828 errors:0 dropped:0 overruns:0 frame:0
TX packets:31546 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:13565103 (12.9 MiB) TX bytes:2431777 (2.3 MiB)
[root@oldboy ~]# ifconfig eth3 | sed -nr 's#^.*dr:(.*) Bc.*$#\1#gp'
10.8.9.65
[root@oldboy ~]# stat /etc/hosts | sed -nr 's#^.*ess: \((.*)\/-.*$#\1#gp'
0644
[root@oldboy ~]# sed -nr 's#([^:]+)(:.*:)(/.*$)#\3\2\1#gp' /etc/passwd | head -4
/bin/bash:x:0:0:root:/root:root
/sbin/nologin:x:1:1:bin:/bin:bin
/sbin/nologin:x:2:2:daemon:/sbin:daemon
/sbin/nologin:x:3:4:adm:/var/adm:adm