Skip to content

Commit 32f0936

Browse files
committed
feat: add grep.md cheatsheet.
1 parent 9cf8737 commit 32f0936

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Quick Reference
4949
[Chmod](./docs/chmod.md)<!--rehype:style=background: rgb(239 68 113/var(\-\-bg\-opacity));-->
5050
[Cron](./docs/cron.md)<!--rehype:style=background: rgb(239 68 68/var(\-\-bg\-opacity));-->
5151
[Git](./docs/git.md)<!--rehype:style=background: rgb(215 89 62/var(\-\-bg\-opacity));-->
52+
[Grep](./docs/grep.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
5253
[find](./docs/find.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
5354
[Sed](./docs/sed.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
5455
[SSH](./docs/ssh.md)<!--rehype:style=background: rgb(99 99 99/var(\-\-bg\-opacity));-->

docs/grep.md

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
Grep 备忘清单
2+
===
3+
4+
本备忘单旨在快速提醒使用命令行程序 grep 所涉及的主要概念,并假设您已经了解其用法。
5+
6+
7+
入门
8+
------
9+
<!--rehype:body-class=cols-5-->
10+
11+
### 使用
12+
<!--rehype:wrap-class=col-span-2-->
13+
14+
搜索标准输出(即文本流)
15+
16+
```shell
17+
$ grep [options] search_string
18+
```
19+
20+
在文件中搜索确切的字符串:
21+
22+
```shell
23+
$ grep [options] search_string path/to/file
24+
```
25+
26+
打印 myfile.txt 中包含字符串“mellon”的行
27+
28+
```shell
29+
$ grep 'mellon' myfile.txt
30+
```
31+
32+
文件名中接受通配符。
33+
34+
35+
### 选项示例
36+
<!--rehype:wrap-class=col-span-3-->
37+
38+
选项 | 示例 | 说明
39+
:-|:-|:-
40+
| `-i` | grep -i ^DA demo.txt | 忘记区分大小写
41+
| `-w` | grep -w "of" demo.txt | 仅搜索完整的单词
42+
| `-A` | grep -A 3 'Exception' error.log | 匹配字符串后显示 3 行
43+
| `-B` | grep -B 4 'Exception' error.log | 在匹配字符串前显示 4 行
44+
| `-C` | grep -C 5 'Exception' error.log | 在匹配字符串周围显示 5 行
45+
| `-r` | grep -r 'github.io' /var/log/nginx/ | 递归搜索 _(在子目录内)_
46+
| `-v` | grep -v 'warning' /var/log/syslog | 返回所有与模式不匹配的行
47+
| `-e` | grep -e '^al' filename | 使用正则表达式 _(以'al'开头的行)_
48+
| `-E` | grep -E 'ja(s\|cks)on' filename | 扩展正则表达式 _(包含 jason 或 jackson 的行)_
49+
| `-c` | grep -c 'error' /var/log/syslog | 计算匹配数
50+
| `-l` | grep -l 'robot' /var/log/* | 打印匹配文件的名称
51+
| `-o` | grep -o search_string filename | 只显示字符串的匹配部分
52+
| `-n` | grep -n "go" demo.txt | 显示匹配的行号
53+
54+
55+
Grep 正则表达式
56+
-------
57+
58+
### 参考
59+
60+
- [Regex syntax](./regex.md) _(jaywcjlove.github.io)_
61+
- [Regex examples](./regex.md#正则表达式示例) _(jaywcjlove.github.io)_
62+
63+
有关更复杂的要求,请参阅完整版的正则表达式备忘单。
64+
65+
### 通配符(Wildcards)
66+
67+
:- | :-
68+
:- | :-
69+
`.` | 任何字符
70+
`?` | 可选且只能出现一次
71+
`*` | 可选的,可以多次出现
72+
`+` | 必需并且可以多次出现
73+
74+
### 量词(Quantifiers)
75+
76+
:- | :-
77+
:- | :-
78+
`{n}` | 前一项恰好出现 n 次
79+
`{n,}` | 上一个项目出现 n 次或更多
80+
`{,m}` | 上一个项目最多出现 n 次
81+
`{n,m}` | 上一项出现在 n 到 m 次之间
82+
83+
### POSIX
84+
85+
:- | :-
86+
:- | :-
87+
`[:alpha:]` | 任何大小写字母
88+
`[:digit:]` | 任何数字
89+
`[:alnum:]` | 任何大小写字母或数字
90+
`[:space:]` | 任何空格
91+
92+
### 字符串
93+
94+
:- | :-
95+
:- | :-
96+
`[A-Z­a-z]` | 任何大小写字母
97+
`[0-9]` | 任何数字
98+
`[0-9­A-Z­a-z]` | 任何大小写字母或数字
99+
100+
101+
### 位置
102+
103+
:- | :-
104+
:- | :-
105+
`^ ` | 行的开头
106+
`$ ` | 行结束
107+
`^$` | 空行
108+
`\<` | 词的开头
109+
`\>` | 词尾
110+
111+
更多示例
112+
----
113+
114+
### 搜索命令行历史记录
115+
116+
```bash
117+
history | grep git
118+
```
119+
120+
输入过 `git` 命令的记录
121+
122+
### 搜索多个文件并查找匹配文本在哪些文件中
123+
124+
```bash
125+
grep -l "text" file1 file2 file3...
126+
```
127+
128+
### 多级目录中对文本进行递归搜索
129+
130+
```bash
131+
grep "text" . -r -n
132+
```
133+
134+
`.` 表示当前目录。
135+
136+
### 搜索结果中包括或者排除指定文件
137+
<!--rehype:wrap-class=row-span-2-->
138+
139+
```bash
140+
# 目录中所有的 .php 和 .html 文件中
141+
# 递归搜索字符 "main()"
142+
grep "main()" . -r --include *.{php,html}
143+
144+
# 在搜索结果中排除所有 README 文件
145+
grep "main()" . -r --exclude "README"
146+
147+
# 在搜索结果中排除 filelist 文件列表里的文件
148+
grep "main()" . -r --exclude-from filelist
149+
```
150+
151+
### 输出包含匹配字符串的行数 -n 选项
152+
<!--rehype:wrap-class=row-span-2-->
153+
154+
```bash
155+
grep "text" -n file_name
156+
#
157+
cat file_name | grep "text" -n
158+
159+
#多个文件
160+
grep "text" -n file_1 file_2
161+
```
162+
163+
### 忽略匹配样式中的字符大小写
164+
165+
```bash
166+
echo "hello world" | grep -i "HELLO"
167+
# hello
168+
```
169+
170+
### 统计文件或文本中包含匹配字符串的行数 -c 选项
171+
172+
```
173+
grep -c "text" file_name
174+
```
175+
176+
另见
177+
----
178+
179+
- [grep 中文文档](https://wangchujiang.com/linux-command/c/grep.html) _(jaywcjlove.github.io)_

scripts/assets/grep.svg

Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)