17個案例帶你3分鐘搞定Linux正則表達式
作者:ansheng
來源:https://blog.ansheng.me/article/examples-of-linux-regular-expressions
正則表達式是一種字元模式,用於在查找過程中匹配製定的字元。
元字元通常在Linux中分為兩類:
Shell元字元,由Linux Shell進行解析;
正則表達式元字元,由vi/grep/sed/awk等文本處理工具進行解析;
正則表達式一般以文本行進行處理,在進行下面實例之前,先為grep命令設置--color參數:
$ alias "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.
文件內容摘錄自<<UNIX/SHELL範例精解第四版>>
$ cat linux.txt
Linux is a good
god assdxw bcvnbvbjk
greatttttt wexcvxc
operaaaating dhfghfvx
gooodfs awrerdxxhkl
gdsystem awxxxx
glad
good
正則表達式元字元
特殊的元字元
擴展的正則表達式
實操
匹配以love開頭的所有行
$ grep "^love"
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"
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"
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" # 對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"
Lovers were all around us. It is springtime. Oh
匹配不在A-Z範圍內的任何字元行,所有的小寫字元
$
grep
"[^A-Z]"
re-fileI 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."
clover. Did you see them? I can only hope love.
匹配空格
$ grep "^$"
匹配任意字元
$ grep ".*"
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}"
groove.
重複o字元至少2次
$
grep
"o{2,}"
re-filegroove.
重複0字元最多2次
$ grep "o{,2}"
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 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 is a good
gdsystem awxxxx
good
分組過濾匹配
$ egrep "g(la|oo)d"
Linux is a good
glad
good
●編號604,輸入編號直達本文
●輸入m獲取文章
目錄
推薦↓↓↓
運維
更多推薦
《
18個技術類微信公眾號
》
涵蓋:程序人生、演算法與數據結構、黑客技術與網路安全、大數據技術、前端開發、Java、Python、Web開發、安卓開發、iOS開發、C/C++、.NET、Linux、資料庫、運維等。


※Linux性能檢測常用的10個基本命令
※詳細到沒朋友,一文幫你理清Linux 用戶與用戶組關係
TAG:Linux學習 |