本文不集中介绍正则表达式的语法。正则表达式的语法会在grep的示例中介绍。
Wikipedia对grep的描述如下:
grep is a command line text search utility originally written for Unix. The name is taken from the first letters in global / regular expression / print, a series of instructions for the ed text editor. The grep command searches files or standard input globally for lines matching a given regular expression, and prints them to the program's standard output.
由此可见,介绍正则表达式,不能不介绍grep。下面先介绍grep的常用选项(以我个人的看法,顺序由常用到不常用)。
1、grep的常用选项
-n, --line-number
Prefix each line of output with the line number within its input
file.-i, --ignore-case
Ignore case distinctions in both the PATTERN and the input
files.
-v, --invert-match
Invert the sense of matching, to select non-matching lines.-V, --version
Print the version number of grep to standard error. This ver-
sion number should be included in all bug reports (see below).-w, --word-regexp
Select only those lines containing matches that form whole
words. The test is that the matching substring must either be
at the beginning of the line, or preceded by a non-word con-
stituent character. Similarly, it must be either at the end of
the line or followed by a non-word constituent character. Word-
constituent characters are letters, digits, and the underscore.-F, --fixed-strings
Interpret PATTERN as a list of fixed strings, separated by new-
lines, any of which is to be matched.
-c, --count
Suppress normal output; instead print a count of matching lines
for each input file. With the -v, --invert-match option (see
below), count non-matching lines.