摘要:$ 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。$ 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。

正則表達式是一種字符模式,用於在查找過程中匹配製定的字符。

元字符通常在Linux中分爲兩類:

  1. Shell元字符,由Linux Shell進行解析;

  2. 正則表達式元字符,由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.

正則表達式元字符

  • 特殊的元字符

  • 擴展的正則表達式

實操

  • 匹配以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

來源:公衆號 IT 釋然雜談

GOPS 2020  全球運維大會 · 2020 深圳站,更多 DevOps、AIOps、自動化運維精彩議題,等你來~

近期好文推薦:

覆蓋全球數十個國家,千億級的監控體系是這樣煉成的

最爽不過拆快遞:京東物流海量倉儲系統智能監控揭祕

滴滴開源夜鶯,新一代企業級監控解決方案!Open-Falcon 之後最新力作

“高效運維”公衆號誠邀廣大技術人員投稿,

投稿郵箱:[email protected],或添加聯繫人微信:135 2116 9787.

GOPS 2020 · 深圳站 

商務合作 周靜:130 7118 2180 (微信同號)李偉:130 2108 2989(微信同號)

票務諮詢 田聰然:132 6188 5689(微信同號)李偉:130 2108 2989(微信同號)

點擊 閱讀原文 ,立即訂票

你點的每個贊,我都認真當成了喜歡

相關文章