grep的用法-飞外

THIS LINE IS THE 1ST UPPER CASE LINE IN THISFILE.this line is the 1st lower case line in thisfile.This Line Has All Its First Character Of The Word With Upper Case.Two lines above this line is empty.And this is the last line.
1.从单个文件中搜索指定的字串

grep的基础用法是如下例的从指定的文件中搜索特定的字串。

语法:

grep"literal_string"filename
$$ grep"this"demo_filethis lineisthe 1st lowercaselineinthis file.Two lines above this lineisempty.Andthisisthe last line.

grep的结果在符合条件的行前将包括文件名。

当文件名包含元字符时,linux shell会将匹配的所有文件作为输入到grep中去。


$$ grep"this"demo_*demo_file:this lineisthe 1st lowercaselineinthis file.demo_file:Two lines above this lineisempty.demo_file:Andthisisthe last line.demo_file1:this lineisthe 1st lowercaselineinthis file.demo_file1:Two lines above this lineisempty.demo_file1:Andthisisthe last line.

也是一个基本用法,

对搜索的字串忽略大小写,因此下例中匹配“the”, “THE” and “The”。


THIS LINEISTHE 1ST UPPERCASELINEINTHIS FILE.this lineisthe 1st lowercaselineinthis file.This Line Has All Its First Character Of The Word With UpperCase.Andthisisthe last line.

如果你能有效地利用正则表达式,这是个很有用的特点。

在下面的例子中,搜索全部以“lines”开始以“empty”结束的字串,

如搜索“lines[之间任意字]empty” ,并且忽略大小写。

$$ grep-i"lines.*empty"demo_fileTwo lines above this lineisempty.

5. 用grep -w搜索整个词,而不是词中的部分字串

使用-w选项搜索一个单词,并且避免搜索到词中的部分字串。

下例搜索"is"。

如果不加-w选项,将显示“is”, “his”, “this” 等所有包含“is”的行。

$$ grep-i"is"demo_fileTHIS LINE IS THE 1ST UPPER CASE LINE IN THISFILE.this line is the 1st lower case line in thisfile.This Line Has All Its First Character Of The Word With Upper Case.Two lines above this line is empty.And this is the last line.

请注意结果中不包含

“This Line Has All Its First Character Of The Word With Upper Case”,

虽然 “This”中包含“is”。

$$ grep-iw"is"demo_fileTHIS LINE IS THE 1ST UPPER CASE LINE IN THISFILE.this line is the 1st lower case line in thisfile.Two lines above this line is empty.And this is the last line.
*e-go to the end of the current word.*E-go to the end of the current WORD.*b-go to the previous(before)word.*B-go to the previous(before)WORD.*w-go to the next word.*W-go to the next WORD.WORD-WORD consists ofasequence of non-blank characters,separated with white space.word-word consists ofasequence of letters,digits and underscores.Example to show the difference between WORD and word*192.168.1.1-single WORD*192.168.1.1-seven words.
$$ grep-A3-i"example"demo_text
Exampletoshow the difference between WORDandword*192.168.1.1-single WORD*192.168.1.1-seven words.
$$ grep-B 2"single WORD"demo_textExampletoshow the difference between WORDandword*192.168.1.1-single WORD
$$ grep-C 2"Example"demo_textword-word consists of a sequence of letters,digitsandunderscores.Exampletoshow the difference between WORDandword*192.168.1.1-single WORD

7.通过GREP_OPTIONS高亮显示搜索的字串

如果你希望搜索的字串高亮显示在结果中,可以试用以下的办法。

通过修改GREP_OPTIONS对搜索字串高亮显示。

$$ export GREP_OPTIONS='--color=auto'GREP_COLOR='100;8'$$ grep this demo_filethis lineisthe 1st lowercaselineinthis file.Two lines above this lineisempty.Andthisisthe last line.

可以使用-v选项显示不匹配搜索字串的行。

下例显示demo_text文件中不包含“go”的行

$$ grep-v"go"demo_text4.Vim Word NavigationYou may wanttodoseveral navigationinrelationtothe words,such as:WORD-WORD consists of a sequence of non-blank characters,separated with whitespace.word-word consists of a sequence of letters,digitsandunderscores.Exampletoshow the difference between WORDandword*192.168.1.1-single WORD*192.168.1.1-seven words.
$$ grep-o"is.*line"demo_file
islineisthe 1st lowercaseline
isline
isisthe last line

注意:以上输出显示的不是行内的位置,而是整个文件中的字节byte位置

15. 用 grep -n 在输出时显示行号

行号从1开始

$$ grep-n"go"demo_text
5:*e-gototheendof the current word.
6:*E-gototheendof the current WORD.
7:*b-gotothe previous(before)word.
8:*B-gotothe previous(before)WORD.
9:*w-gotothenextword.
10:*W-gotothenextWORD.