2019独角兽企业重金招聘Python工程师标准>>>
首先,新建如下文件demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
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 "literal_string" filename
$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
2 在多个文件中搜索指定字符串
语法:grep "string" FILE_PATTERN
$cp demo_file demo_file1
$grep "this" demo_*#结果如下
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.
3 用grep -i忽略大小写 语法:grep -i "string" FILE
$grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.
4 匹配正则表达式 语法:grep "REGEX" filename
$ grep "lines.*empty" demo_file
Two lines above this line is empty.
补充一点正则:
? 前面的字段出现0或1次
* 前面的字段出现0或多次
+ 前面的字段出现1或多次
{n} 前面的字段出现n次
{n,} 前面的字段出现n或者更多次
{,m} 前面的字段最多出现m次
{n,m} 前面的字段至少出现n次,最多出现m次
5 完整匹配,忽略字串语法:grep -w "word" FILENAME
$grep -i "is" demo_file
#除匹配is之外,还会匹配his this等
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
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.
为防止这个现象,用-w
$ grep -iw "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
6 查看匹配行的前、后和周围(after/behind/around) 先新建demo_text
4. Vim Word NavigationYou may want to do several navigation in relation to the words, such as:* 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 of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence 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.
6.1 显示后N行 语法:grep -A <N> "string" FILENAME
$ grep -A 3 -i "example" demo_text
Example to show the difference between WORD and word* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
6.2 显示前N行 语法:grep -B <N> "string" FILENAME
$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word* 192.168.1.1 - single WORD
6.3 显示周围的行 直接上例子
$ grep -C 2 "Example" demo_text
word - word consists of a sequence of letters, digits and underscores.Example to show the difference between WORD and word* 192.168.1.1 - single WORD
7 高亮显示
export GREP_OPTIONS='--color=auto' GREP_COLOR='1;32'
搜索关键字,匹配之后会显示绿色。 如果不想高亮显示,把auto改成none即可。
8 递归查找
语法:grep -r "string" .
会遍历指定目录下所有文件及子目录下所有文件。
9 用-v显示不匹配的行
语法:grep -v "string" FILENAME
下面的例子会匹配所有不包含go的行。
$grep -v "go" demo_txt
4. Vim Word NavigationYou may want to do several navigation in relation to the words, such as:WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence 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.
10 显示不匹配所有规则的行
$ cat test-file.txt
a
b
c
d$ grep -v -e "a" -e "b" -e "c" test-file.txt
d
11 计算匹配行数 语法:grep -c "pattern" filename
和-v连用就是计算不匹配的行数
12 只显示匹配的文件名
语法:grep -l "pattern" file*
注意:-l是小写的L
$ grep -l this demo_*
demo_file
demo_file1
13 只显示匹配的字符串(使用正则的时候特别有用)
$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line
14 显示匹配的位置
$ cat temp-file.txt
12345
12345$ grep -o -b "3" temp-file.txt
2:3
8:3
15 显示行号
$ grep -n "go" demo_text
5: * e - go to the end of the current word.
6: * E - go to the end of the current WORD.
7: * b - go to the previous (before) word.
8: * B - go to the previous (before) WORD.
9: * w - go to the next word.
10: * W - go to the next WORD.