標籤: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