diff --git a/docs/grep.html b/docs/grep.html new file mode 100644 index 00000000..f404b46a --- /dev/null +++ b/docs/grep.html @@ -0,0 +1,318 @@ + + + + +Grep 备忘清单 + & grep cheatsheet & Quick Reference + + + + + + +

Grep 备忘清单

+

本备忘单旨在快速提醒使用命令行程序 grep 所涉及的主要概念,并假设您已经了解其用法。

+

入门

+ +

使用

+ +

搜索标准输出(即文本流)

+
$ grep [options] search_string
+
+

在文件中搜索确切的字符串:

+
$ grep [options] search_string path/to/file
+
+

打印 myfile.txt 中包含字符串“mellon”的行

+
$ grep 'mellon' myfile.txt
+
+

文件名中接受通配符。

+

选项示例

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
选项示例说明
-igrep -i ^DA demo.txt忘记区分大小写
-wgrep -w "of" demo.txt仅搜索完整的单词
-Agrep -A 3 'Exception' error.log匹配字符串后显示 3 行
-Bgrep -B 4 'Exception' error.log在匹配字符串前显示 4 行
-Cgrep -C 5 'Exception' error.log在匹配字符串周围显示 5 行
-rgrep -r 'github.io' /var/log/nginx/递归搜索 (在子目录内)
-vgrep -v 'warning' /var/log/syslog返回所有与模式不匹配的行
-egrep -e '^al' filename使用正则表达式 (以'al'开头的行)
-Egrep -E 'ja(s|cks)on' filename扩展正则表达式 (包含 jason 或 jackson 的行)
-cgrep -c 'error' /var/log/syslog计算匹配数
-lgrep -l 'robot' /var/log/*打印匹配文件的名称
-ogrep -o search_string filename只显示字符串的匹配部分
-ngrep -n "go" demo.txt显示匹配的行号
+

Grep 正则表达式

+

参考

+ +

有关更复杂的要求,请参阅完整版的正则表达式备忘单。

+

通配符(Wildcards)

+ + + + + + + + + + + + + + + + + + + + + + + + + +
:-:-
.任何字符
?可选且只能出现一次
*可选的,可以多次出现
+必需并且可以多次出现
+

量词(Quantifiers)

+ + + + + + + + + + + + + + + + + + + + + + + + + +
:-:-
{n}前一项恰好出现 n 次
{n,}上一个项目出现 n 次或更多
{,m}上一个项目最多出现 n 次
{n,m}上一项出现在 n 到 m 次之间
+

POSIX

+ + + + + + + + + + + + + + + + + + + + + + + + + +
:-:-
[:alpha:]任何大小写字母
[:digit:]任何数字
[:alnum:]任何大小写字母或数字
[:space:]任何空格
+

字符串

+ + + + + + + + + + + + + + + + + + + + + +
:-:-
[A-Z­a-z]任何大小写字母
[0-9]任何数字
[0-9­A-Z­a-z]任何大小写字母或数字
+

位置

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
:-:-
^ 行的开头
$ 行结束
^$空行
\<词的开头
\>词尾
+

更多示例

+

搜索命令行历史记录

+
history | grep git
+
+

输入过 git 命令的记录

+

搜索多个文件并查找匹配文本在哪些文件中

+
grep -l "text" file1 file2 file3...
+
+

多级目录中对文本进行递归搜索

+
grep "text" . -r -n
+
+

. 表示当前目录。

+

搜索结果中包括或者排除指定文件

+ +
# 目录中所有的 .php 和 .html 文件中
+# 递归搜索字符 "main()"
+grep "main()" . -r --include *.{php,html}
+
+# 在搜索结果中排除所有 README 文件
+grep "main()" . -r --exclude "README"
+
+# 在搜索结果中排除 filelist 文件列表里的文件
+grep "main()" . -r --exclude-from filelist
+
+

输出包含匹配字符串的行数 -n 选项

+ +
grep "text" -n file_name
+# 或
+cat file_name | grep "text" -n
+
+#多个文件
+grep "text" -n file_1 file_2
+
+

忽略匹配样式中的字符大小写

+
echo "hello world" | grep -i "HELLO"
+# hello
+
+

统计文件或文本中包含匹配字符串的行数 -c 选项

+
grep -c "text" file_name
+
+

另见

+ +
+ diff --git a/index.html b/index.html index 57c19ffe..accfea0d 100644 --- a/index.html +++ b/index.html @@ -156,6 +156,10 @@ Git + + + +Grep find