作者:ansheng
來源:https://blog.ansheng.me/article/examples-of-linux-regular-expressions
正則運算式是一種字元樣式,用於在查詢過程中匹配製定的字元。
元字元通常在Linux中分為兩類:
Shell元字元,由Linux Shell進行解析;
正則運算式元字元,由vi/grep/sed/awk等文字處理工具進行解析;
正則運算式一般以文字行進行處理,在進行下麵實體之前,先為grep命令設定–color引數:
$ alias grep='grep --color=auto'
這樣每次過濾出來的字串都會帶色彩了。
在開始之前還需要做一件事情,就是建立一個測試用的re-file檔案,內容如下:
$ cat re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
檔案內容摘錄自<
>
$ cat linux.txt
Linux is a good
god assdxw bcvnbvbjk
greatttttt wexcvxc
operaaaating dhfghfvx
gooodfs awrerdxxhkl
gdsystem awxxxx
glad
good
正則運算式元字元
-
特殊的元字元
-
擴充套件的正則運算式
實操
匹配以love開頭的所有行
$ grep '^love' re-file
love, how much I adore you. Do you know
匹配love結尾的所有行
$ grep 'love$' re-file
clover. Did you see them? I can only hope love.
匹配以l開頭,中間包含兩個字元,結尾是e的所有行
$ grep 'l..e' re-file
I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love.
is forever. I live for you. It's hard to get back in the
匹配0個或多個空行,後面是love的字元
$ grep ' *love' re-file
I had a lovely time on our little picnic.
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love.
匹配love或Love
$ grep '[Ll]ove' re-file # 對l不區分大小寫
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love.
匹配A-Z的字母,其次是ove
$ grep '[A-Z]ove' re-file
Lovers were all around us. It is springtime. Oh
匹配不在A-Z範圍內的任何字元行,所有的小寫字元
$ grep '[^A-Z]' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
匹配love.
$ grep 'love\.' re-file
clover. Did you see them? I can only hope love.
匹配空格
$ grep '^$' re-file
匹配任意字元
$ grep '.*' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
前面o字元重覆2到4次
$ grep 'o\{2,4\}' re-file
groove.
重覆o字元至少2次
$ grep 'o\{2,\}' re-file
groove.
重覆0字元最多2次
$ grep 'o\{,2\}' re-file
I had a lovely time on our little picnic.
Lovers were all around us. It is springtime. Oh
love, how much I adore you. Do you know
the extent of my love? Oh, by the way, I think
I lost my gloves somewhere out in that field of
clover. Did you see them? I can only hope love.
is forever. I live for you. It's hard to get back in the
groove.
重覆前一個字元一個或一個以
$ egrep "go+d" linux.txt
Linux is a good
god assdxw bcvnbvbjk
gooodfs awrerdxxhkl
good
#####0個或者一個字元
ansheng@Ubuntu:/tmp$ egrep "go?d" linux.txt
god assdxw bcvnbvbjk
gdsystem awxxxx
或,查詢多個字串
$ egrep "gd|good" linux.txt
Linux is a good
gdsystem awxxxx
good
分組過濾匹配
$ egrep "g(la|oo)d" linux.txt
Linux is a good
glad
good
●編號604,輸入編號直達本文
●輸入m獲取文章目錄
運維
更多推薦《18個技術類微信公眾號》
涵蓋:程式人生、演演算法與資料結構、駭客技術與網路安全、大資料技術、前端開發、Java、Python、Web開發、安卓開發、iOS開發、C/C++、.NET、Linux、資料庫、運維等。