常用的 grep 命令
Nov 27, 2018 00:00 · 1203 words · 3 minute read
先创建一个 demo 文档
$ cat 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. 在多个文件中查找特定的字符串
先复制一份 demo_fole,grep 查找匹配特定模式的文件。
语法: 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 "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
下面的例子中,搜索所有以 lines 开头并且以 empty 结尾的内容:
$ grep "lines.*empty" demo_file
Two lines above this line is empty.
- ? 可选并且最多出现一次
- * 通配符
- + 至少出现一次
- {n} 精确出现 n 次
- {n,} 至少出现 n 次
- {,m} 最多出现 m 次
- {n,m} 最少出现 n 次,最多出现 m 次
5. 全词匹配,而不是子串
当你查找 is
,如果不带任何选项,his
、this
等任何含有 is
的内容都会被查出来。
$ grep -i "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.
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
后,只查找完整的 is
单词:
$ 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. 显示行数
当对一个大文件做 grep 搜索时,有时候只需要显示
再创建一个 demo 文档。
$ cat demo_text
4. Vim Word Navigation
You 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
下面的例子打印出查找结果,还有之后的 3 行:
$ 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.2 显示查找结果上面和下面的 N 行
语法 grep -C <N> "string" FILENAME
-C
选项会同时打印出匹配结果两边的 N 行。
$ 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. 高亮查找结果
如果你想要高亮标记出匹配的部分,要先设置下 GREP_OPTIONS 环境变量。
$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
$ 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.
8. 递归查找所有文件
查找当前目录下的所有文件和子目录。
语法:$ grep -r "ramesh" *
9. 反向匹配
如果你想要显示所有不匹配的内容,使用 -v
选项:
$ grep -v "go" demo_text
4. Vim Word Navigation
You 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. 统计匹配数量
语法:grep -c "pattern" filename
$ grep -c "go" demo_text
6
统计出匹配的行数:
$ grep -c this demo_file
3
统计出不匹配的行数:
$ grep -v -c this demo_file
4
11. 只显示存在匹配的文件名
如果只想要包含匹配内容的文件名,带上 -l
参数。在多文件作为输入的查找中会很好用。
$ grep -l this demo_*
demo_file
demo_file1