【SHELL】grep 命令用法

linux 命令行查询 grep 用法信息

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)

 

这些选项定义了 grep 使用哪种模式来匹配文本:

  • -E, --extended-regexp:启用扩展正则表达式(ERE),支持更多复杂的正则语法,例如 +, ?, {} 等。

    示例

    grep -E 'file(s)?' file.txt

    匹配 filefiles

  • -F, --fixed-strings:将模式视为普通字符串,而非正则表达式。这种方式忽略特殊字符的含义。

    示例

    grep -F '[+] error' file.txt

    直接匹配包含 [+] error 字符串的行,不将 [] 视为正则表达式的字符集。

  • -G, --basic-regexp:使用基本正则表达式(BRE),这是默认的行为。

    示例

    grep 'file[0-9]' file.txt

    匹配 file 后面跟一个数字的行。

  • -P, --perl-regexp:使用 Perl 风格的正则表达式语法,这允许更加复杂的正则表达式模式。

    示例

    grep -P '\d+' file.txt

    匹配包含一个或多个数字的行,-P 允许使用 \d(数字)这样的 Perl 正则表达式语法。

  • -e, --regexp=PATTERNS:指定用于匹配的模式(可以使用多次来指定多个模式)。

    示例

    grep -e 'error' -e 'warning' file.txt

    匹配包含 errorwarning 的行。

  • -f, --file=FILE:从文件中读取模式,每一行作为一个模式。

    示例

    grep -f patterns.txt file.txt

    patterns.txt 文件中的每一行都是一个模式,grep 会逐一进行匹配。

  • -i, --ignore-case:忽略大小写的区别。

    示例

    grep -i 'error' file.txt

    匹配 error, Error, ERROR 等形式。

  • -w, --word-regexp:只匹配完整的单词,而不是部分匹配。

    示例

    grep -w 'is' file.txt

    只匹配 is 作为独立单词的行,而不匹配 thishis

  • -x, --line-regexp:只匹配整个行。

    示例

    grep -x 'hello' file.txt

    只匹配完全是 hello 的行,而不会匹配包含 hello 的其他行。

输出控制

这些选项控制 grep 的输出行为:

  • -n, --line-number:打印匹配行的行号。

    示例

    grep -n 'error' file.txt

    输出包含 error 的行及其行号。

  • -c, --count:只打印匹配的行数。

    示例

    grep -c 'error' file.txt

    输出文件中包含 error 的行数。

  • -o, --only-matching:只显示匹配的部分,而不是整行。

    示例

    grep -o 'error' file.txt

    只显示 error 出现的次数和位置。

  • -H, --with-filename:在输出行前显示文件名(用于多个文件时)。

    示例

    grep -H 'error' *.txt

    匹配多个文件,输出格式为 文件名:内容

  • -h, --no-filename:当搜索多个文件时,抑制文件名输出。

    示例

    grep -h 'error' *.txt

    只显示匹配的行,不显示文件名。

  • -l, --files-with-matches:只输出包含匹配行的文件名。

    示例

    grep -l 'error' *.txt

    只输出包含 error 的文件名。

上下文控制

这些选项允许显示匹配行前后的一些额外内容,帮助理解上下文:

  • -A NUM, --after-context=NUM:在匹配行后显示 NUM 行。

    示例

    grep -A 3 'error' file.txt

    输出匹配 error 的行及其后 3 行。

  • -B NUM, --before-context=NUM:在匹配行前显示 NUM 行。

    示例

    grep -B 3 'error' file.txt

    输出匹配 error 的行及其前 3 行。

  • -C NUM, --context=NUM:在匹配行前后各显示 NUM 行。

    示例

    grep -C 2 'error' file.txt

    输出匹配 error 的行及其前后 2 行。

其他常用选项

  • -v, --invert-match:显示不匹配模式的行(反转匹配)。

    示例

    grep -v 'error' file.txt

    输出文件中不包含 error 的行。

  • -r, --recursive:递归搜索目录中的文件。

    示例

    grep -r 'error' /path/to/directory

    递归搜索指定目录中的所有文件。

  • --color:高亮显示匹配到的字符串。

    示例

    grep --color 'error' file.txt

    在输出中高亮显示匹配的部分。

例子总结

  1. 忽略大小写匹配 "error",并显示匹配的行号:

    grep -in 'error' file.txt
  2. 递归搜索包含 "error" 的所有文件,并只显示文件名:

    grep -rl 'error' /path/to/directory
  3. 只显示匹配到的部分,并高亮显示 "success":

    grep -o --color 'success' file.txt
  4. 搜索包含 "failed" 的行,并显示前后 2 行:

    grep -C 2 'failed' file.txt
  5. 显示不包含 "warning" 的行:

    grep -v 'warning' file.txt

 

posted @   壹点灵异  阅读(59)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示