# grep
# 目录
[TOC]
<font style="background:pink">
1
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。
- grep家族包括
- grep
- egrep
- egrep和fgrep的命令只跟grep有很小不同。
- egrep是grep的扩展,支持更多的re元字符
- fgrep
- fgrep就是fixed grep或fast grep,它们把所有的字母都看作单词,也就是说,正则表达式中的元字符表示回其自身的字面意义,不再特殊。
- linux使用GNU版本的grep。它功能更强,可以通过
-G
、-E
、-F
命令行选项来使用egrep和fgrep的功能
# 1.格式和主要参数
grep [options]
主要参数: grep --help可查看
-c:只输出匹配行的计数。「count,v.计数」
-i:不区分大小写。「ignore,v.忽略」
-h:查询多文件时不显示文件名。「我也不晓得这个有啥用,文件名不是帮助我们找源代码的嘛。。」
-l:查询多文件时只输出包含匹配字符的文件名。「感觉和上面选项互补」
-n:显示匹配行及行号。「num」good,更好的帮助定位源代码位置
-s:不显示不存在或无匹配文本的错误信息。
-v:显示不包含匹配文本的所有行。「哈哈,这个要干啥?压榨服务器性能?」
--color=auto :可以将找到的关键词部分加上颜色的显示。
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
- grep跳过某文件夹
- 虽然help中说“--exclude”可以忽略文件和目录,可是实际测试中发现并不能忽略目录
- 所以,要排除目录,还得用“--exclude-dir”
XXXX@kXXXXX:~/whoway$ ls
riscv_gcc onw two
XXXX@kXXXXX:~/whoway$ grep --exclude-dir=riscv_gcc/ aaa *
grep: onw: Is a directory
grep: two: Is a directory
XXXX@kXXXXX:~/whoway$ grep --exclude-dir=riscv_gcc/ aaa * -r
onw/one.c:aaa
two/twoo.c:aaa
其实grep --exclude-dir riscv_gcc/ aaa * -r
这个的=号不加也行
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 2.官方翻译grep --help
Usage: grep [OPTION]... PATTERNS [FILE]...
Search for PATTERNS in each FILE.
Example: grep -i 'hello world' menu.h main.c
PATTERNS can contain multiple patterns separated by newlines.
Pattern selection and interpretation:
-E, --extended-regexp PATTERNS are extended regular expressions
-F, --fixed-strings PATTERNS are strings
-G, --basic-regexp PATTERNS are basic regular expressions
-P, --perl-regexp PATTERNS are Perl regular expressions
-e, --regexp=PATTERNS use PATTERNS for matching
-f, --file=FILE take PATTERNS from FILE
-i, --ignore-case ignore case distinctions in patterns and data
--no-ignore-case do not ignore case distinctions (default)
-w, --word-regexp match only whole words
-x, --line-regexp match only whole lines
-z, --null-data a data line ends in 0 byte, not newline
Miscellaneous:「杂项」
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version display version information and exit
--help display this help text and exit
Output control:「输出控制」
-m, --max-count=NUM stop after NUM selected lines
-b, --byte-offset print the byte offset with output lines
-n, --line-number print line number with output lines
--line-buffered flush output on every line
-H, --with-filename print file name with output lines
-h, --no-filename suppress the file name prefix on output
--label=LABEL use LABEL as the standard input file name prefix
-o, --only-matching show only nonempty parts of lines that match
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE;
TYPE is 'binary', 'text', or 'without-match'
-a, --text equivalent to --binary-files=text
-I equivalent to --binary-files=without-match
-d, --directories=ACTION how to handle directories;
ACTION is 'read', 'recurse', or 'skip'
-D, --devices=ACTION how to handle devices, FIFOs and sockets;
ACTION is 'read' or 'skip'
-r, --recursive like --directories=recurse
-R, --dereference-recursive likewise, but follow all symlinks
--include=GLOB search only files that match GLOB (a file pattern)
--exclude=GLOB skip files that match GLOB
--exclude-from=FILE skip files that match any file pattern from FILE
--exclude-dir=GLOB skip directories that match GLOB
-L, --files-without-match print only names of FILEs with no selected lines
-l, --files-with-matches print only names of FILEs with selected lines
-c, --count print only a count of selected lines per FILE
-T, --initial-tab make tabs line up (if needed)
-Z, --null print 0 byte after FILE name
Context control:「上下文控制、上下文控制」
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
-NUM same as --context=NUM
--color[=WHEN],
--colour[=WHEN] use markers to highlight the matching strings;
WHEN is 'always', 'never', or 'auto'
-U, --binary do not strip CR characters at EOL (MSDOS/Windows)
When FILE is '-', read standard input. With no FILE, read '.' if
recursive, '-' otherwise. With fewer than two FILEs, assume -h.
Exit status is 0 if any line (or file if -L) is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.
Report bugs to: [email protected]
GNU grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <https://www.gnu.org/gethelp/>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 3.egrep加上-n显示行数
[root@iZwz9ivqsnyj6hi71t3pamZ ~]# egrep 'now6' test.cpp
// int now6=now5+tag[6];
// if(now6==n)
// else if(now6>n)
[root@iZwz9ivqsnyj6hi71t3pamZ ~]# egrep 'now6' test.cpp -n
87:// int now6=now5+tag[6];
88:// if(now6==n)
93:// else if(now6>n)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9