Regular expressions are divided into basic regular expressions and extended regular expressions.
Typically, grep uses a basic regular expression, and if you want to use an extended regular expression, you can use the-e option, which is equivalent to Egrep
Some other options for grep:
-A N: Displays the n rows after the matching line, (after).
-B N: Displays the n rows preceding the matching line, (before).
-C N: Displays the front and back n rows. (context)
Example 1:
[[email protected] tmp]# grep--color-a 1 ' ^cpu MHz '/proc/cpuinfocpu mhz:2392.390cache size:3072 kb[[em AIL protected] tmp]#
[[email protected] tmp]# grep --color -a 1 -b 2 ' ^cpu MHz ' /proc/cpuinfomodel 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/cpuinfomodel name : intel (R) core (TM) i3-2370M CPU @ 2.40GHzstepping : 7cpu MHz : 2392.390cache size : 3072 kbfpu : yes [[Email protected] tmp]#
An extended regular expression:
In the extended regular expression inside the metacharacters and basic regular expressions inside the meta-characters are basically the same, the difference is mainly a few:
+: Matches the previous character appears 1 or more times.
{N,m}: The previous character appears N to M times.
|: Represents or. If a|b, the match is either A or B.
(): Indicates grouping, no backslash is required.
[[email protected] tmp]# grep-e ' (c|c) at ' Aacatcat[[email protected] tmp]#
Matches the number between 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 '
[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] ~]#
This article from "Love Open source, happy to share!" "Blog, be sure to keep this provenance http://hezhanglinux.blog.51cto.com/10861477/1711027
Extended usage of Text Processing command series--grep