feat: add grep.md
cheatsheet.
This commit is contained in:
parent
9cf8737419
commit
32f0936c36
@ -49,6 +49,7 @@ Quick Reference
|
||||
[Chmod](./docs/chmod.md)<!--rehype:style=background: rgb(239 68 113/var(\-\-bg\-opacity));-->
|
||||
[Cron](./docs/cron.md)<!--rehype:style=background: rgb(239 68 68/var(\-\-bg\-opacity));-->
|
||||
[Git](./docs/git.md)<!--rehype:style=background: rgb(215 89 62/var(\-\-bg\-opacity));-->
|
||||
[Grep](./docs/grep.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
|
||||
[find](./docs/find.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
|
||||
[Sed](./docs/sed.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
|
||||
[SSH](./docs/ssh.md)<!--rehype:style=background: rgb(99 99 99/var(\-\-bg\-opacity));-->
|
||||
|
179
docs/grep.md
Normal file
179
docs/grep.md
Normal file
@ -0,0 +1,179 @@
|
||||
Grep 备忘清单
|
||||
===
|
||||
|
||||
本备忘单旨在快速提醒使用命令行程序 grep 所涉及的主要概念,并假设您已经了解其用法。
|
||||
|
||||
|
||||
入门
|
||||
------
|
||||
<!--rehype:body-class=cols-5-->
|
||||
|
||||
### 使用
|
||||
<!--rehype:wrap-class=col-span-2-->
|
||||
|
||||
搜索标准输出(即文本流)
|
||||
|
||||
```shell
|
||||
$ grep [options] search_string
|
||||
```
|
||||
|
||||
在文件中搜索确切的字符串:
|
||||
|
||||
```shell
|
||||
$ grep [options] search_string path/to/file
|
||||
```
|
||||
|
||||
打印 myfile.txt 中包含字符串“mellon”的行
|
||||
|
||||
```shell
|
||||
$ grep 'mellon' myfile.txt
|
||||
```
|
||||
|
||||
文件名中接受通配符。
|
||||
|
||||
|
||||
### 选项示例
|
||||
<!--rehype:wrap-class=col-span-3-->
|
||||
|
||||
选项 | 示例 | 说明
|
||||
:-|:-|:-
|
||||
| `-i` | grep -i ^DA demo.txt | 忘记区分大小写
|
||||
| `-w` | grep -w "of" demo.txt | 仅搜索完整的单词
|
||||
| `-A` | grep -A 3 'Exception' error.log | 匹配字符串后显示 3 行
|
||||
| `-B` | grep -B 4 'Exception' error.log | 在匹配字符串前显示 4 行
|
||||
| `-C` | grep -C 5 'Exception' error.log | 在匹配字符串周围显示 5 行
|
||||
| `-r` | grep -r 'github.io' /var/log/nginx/ | 递归搜索 _(在子目录内)_
|
||||
| `-v` | grep -v 'warning' /var/log/syslog | 返回所有与模式不匹配的行
|
||||
| `-e` | grep -e '^al' filename | 使用正则表达式 _(以'al'开头的行)_
|
||||
| `-E` | grep -E 'ja(s\|cks)on' filename | 扩展正则表达式 _(包含 jason 或 jackson 的行)_
|
||||
| `-c` | grep -c 'error' /var/log/syslog | 计算匹配数
|
||||
| `-l` | grep -l 'robot' /var/log/* | 打印匹配文件的名称
|
||||
| `-o` | grep -o search_string filename | 只显示字符串的匹配部分
|
||||
| `-n` | grep -n "go" demo.txt | 显示匹配的行号
|
||||
|
||||
|
||||
Grep 正则表达式
|
||||
-------
|
||||
|
||||
### 参考
|
||||
|
||||
- [Regex syntax](./regex.md) _(jaywcjlove.github.io)_
|
||||
- [Regex examples](./regex.md#正则表达式示例) _(jaywcjlove.github.io)_
|
||||
|
||||
有关更复杂的要求,请参阅完整版的正则表达式备忘单。
|
||||
|
||||
### 通配符(Wildcards)
|
||||
|
||||
:- | :-
|
||||
:- | :-
|
||||
`.` | 任何字符
|
||||
`?` | 可选且只能出现一次
|
||||
`*` | 可选的,可以多次出现
|
||||
`+` | 必需并且可以多次出现
|
||||
|
||||
### 量词(Quantifiers)
|
||||
|
||||
:- | :-
|
||||
:- | :-
|
||||
`{n}` | 前一项恰好出现 n 次
|
||||
`{n,}` | 上一个项目出现 n 次或更多
|
||||
`{,m}` | 上一个项目最多出现 n 次
|
||||
`{n,m}` | 上一项出现在 n 到 m 次之间
|
||||
|
||||
### POSIX
|
||||
|
||||
:- | :-
|
||||
:- | :-
|
||||
`[:alpha:]` | 任何大小写字母
|
||||
`[:digit:]` | 任何数字
|
||||
`[:alnum:]` | 任何大小写字母或数字
|
||||
`[:space:]` | 任何空格
|
||||
|
||||
### 字符串
|
||||
|
||||
:- | :-
|
||||
:- | :-
|
||||
`[A-Za-z]` | 任何大小写字母
|
||||
`[0-9]` | 任何数字
|
||||
`[0-9A-Za-z]` | 任何大小写字母或数字
|
||||
|
||||
|
||||
### 位置
|
||||
|
||||
:- | :-
|
||||
:- | :-
|
||||
`^ ` | 行的开头
|
||||
`$ ` | 行结束
|
||||
`^$` | 空行
|
||||
`\<` | 词的开头
|
||||
`\>` | 词尾
|
||||
|
||||
更多示例
|
||||
----
|
||||
|
||||
### 搜索命令行历史记录
|
||||
|
||||
```bash
|
||||
history | grep git
|
||||
```
|
||||
|
||||
输入过 `git` 命令的记录
|
||||
|
||||
### 搜索多个文件并查找匹配文本在哪些文件中
|
||||
|
||||
```bash
|
||||
grep -l "text" file1 file2 file3...
|
||||
```
|
||||
|
||||
### 多级目录中对文本进行递归搜索
|
||||
|
||||
```bash
|
||||
grep "text" . -r -n
|
||||
```
|
||||
|
||||
`.` 表示当前目录。
|
||||
|
||||
### 搜索结果中包括或者排除指定文件
|
||||
<!--rehype:wrap-class=row-span-2-->
|
||||
|
||||
```bash
|
||||
# 目录中所有的 .php 和 .html 文件中
|
||||
# 递归搜索字符 "main()"
|
||||
grep "main()" . -r --include *.{php,html}
|
||||
|
||||
# 在搜索结果中排除所有 README 文件
|
||||
grep "main()" . -r --exclude "README"
|
||||
|
||||
# 在搜索结果中排除 filelist 文件列表里的文件
|
||||
grep "main()" . -r --exclude-from filelist
|
||||
```
|
||||
|
||||
### 输出包含匹配字符串的行数 -n 选项
|
||||
<!--rehype:wrap-class=row-span-2-->
|
||||
|
||||
```bash
|
||||
grep "text" -n file_name
|
||||
# 或
|
||||
cat file_name | grep "text" -n
|
||||
|
||||
#多个文件
|
||||
grep "text" -n file_1 file_2
|
||||
```
|
||||
|
||||
### 忽略匹配样式中的字符大小写
|
||||
|
||||
```bash
|
||||
echo "hello world" | grep -i "HELLO"
|
||||
# hello
|
||||
```
|
||||
|
||||
### 统计文件或文本中包含匹配字符串的行数 -c 选项
|
||||
|
||||
```
|
||||
grep -c "text" file_name
|
||||
```
|
||||
|
||||
另见
|
||||
----
|
||||
|
||||
- [grep 中文文档](https://wangchujiang.com/linux-command/c/grep.html) _(jaywcjlove.github.io)_
|
3
scripts/assets/grep.svg
Normal file
3
scripts/assets/grep.svg
Normal file
@ -0,0 +1,3 @@
|
||||
<svg viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
|
||||
<path d="M5 3a2 2 0 0 0-2 2v4.758A4.5 4.5 0 0 1 9.973 13h3.529a.5.5 0 0 1 0 1h-3.53a4.499 4.499 0 0 1-.411 1.439L11.122 17h3.879a2 2 0 0 0 2-2V5a2 2 0 0 0-2-2H5Zm2.5 4.25a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0Zm2-.25h4a.5.5 0 0 1 0 1h-4a.5.5 0 0 1 0-1ZM9 10.5a.5.5 0 0 1 .5-.5h4a.5.5 0 0 1 0 1h-4a.5.5 0 0 1-.5-.5Zm-6.615 1.405A3.5 3.5 0 0 0 7.6 16.3l2.543 2.558a.5.5 0 0 0 .707-.708L8.3 15.6a3.5 3.5 0 1 0-5.916-3.695Zm5.194.206a2.5 2.5 0 1 1-4.157 2.778 2.5 2.5 0 0 1 4.157-2.778Z" />
|
||||
</svg>
|
After Width: | Height: | Size: 601 B |
Loading…
x
Reference in New Issue
Block a user