grep的擴充用法

來源:互聯網
上載者:User

標籤:egrep、 grep -a

Regex分為基本的Regex和擴充的Regex。

通常情況下,grep使用的是基本的Regex,如果要使用擴充的Regex,可以用-E選項,等同於egrep

grep的其他一些選項:

-A n:顯示匹配行後面的n行,(after)。

-B n:顯示匹配行前面的n行,(before)。

-C n:顯示前後各n行。(context)

樣本1:

[[email protected] tmp]# grep --color -A 1 ‘^cpu MHz‘ /proc/cpuinfo cpu MHz         : 2392.390cache size      : 3072 KB[[email protected] tmp]#
[[email protected] tmp]# grep --color -A 1 -B 2 ‘^cpu MHz‘ /proc/cpuinfo model name      : Intel(R) Core(TM) i3-2370M CPU @ 2.40GHzstepping        : 7cpu MHz         : 2392.390cache size      : 3072 KB[[email protected] tmp]#
[[email protected] tmp]# grep --color -C 2 ‘^cpu MHz‘ /proc/cpuinfo model name      : Intel(R) Core(TM) i3-2370M CPU @ 2.40GHzstepping        : 7cpu MHz         : 2392.390cache size      : 3072 KBfpu             : yes[[email protected] tmp]#


擴充的Regex:

在擴充的正則表示式裡面所使用的元字元和基本的Regex裡面的元字元基本上都相同,區別主要有一下幾個:

+:匹配前一個字元出現1次或多次。

{n,m}:前一個字元出現n到m次。

|:表示或。如a|b,則表示的是匹配a或b。

():表示分組,不需要使用反斜線。

樣本:

[[email protected] tmp]# grep -E ‘(C|c)at‘ aacatCat[[email protected] tmp]#


匹配1-255之間的數:

grep -E --color ‘\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b‘


查看IP資訊:

[[email protected] ~]# ifconfig bond0 | grep ‘inet addr‘ | grep -E --color -o ‘(\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.\b){3}\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b‘ 172.17.100.252172.17.100.255255.255.255.0[[email protected] ~]#



本文出自 “HeZhang” 部落格,請務必保留此出處http://hezhang.blog.51cto.com/1347601/1435168

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.