首页 > 系统相关 >【SHELL】grep 命令用法

【SHELL】grep 命令用法

时间:2024-10-21 20:35:28浏览次数:1  
标签:SHELL grep -- 用法 file error 匹配 txt

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

 

标签:SHELL,grep,--,用法,file,error,匹配,txt
From: https://www.cnblogs.com/skullboyer/p/18490297

相关文章

  • shell与会话
    当您在命令行中运行一个脚本(如`./gotobin`)时,该脚本会在一个新的子Shell中执行。这意味着脚本中的所有命令(包括`cd`命令)都仅在这个子Shell的上下文中运行。一旦脚本执行完毕,子Shell就会关闭,并且您会返回到原始的交互式Shell会话中,该会话的工作目录保持不变。这是因为`......
  • rolling 用法
    Pandas库中的`rolling`方法是一种强大的数据处理工具,主要用于执行基于滑动窗口的计算,这在时间序列数据或数据框中非常常见。以下是对`rolling`方法及其支持的函数的综合概述:###1.`rolling`方法的基本概念和用法`rolling`方法用于创建一个滚动窗口对象,该对象可以应用于数据框......
  • Shell 主要的作用是什么
    Shell主要的作用:1.提供与操作系统交互的命令行界面;2.执行和管理系统命令和程序;3.脚本编程和自动化任务;4.环境配置和用户管理;5.管理文件和目录;6.进程控制和任务调度。Shell是用户与操作系统进行交互的主要界面,为用户提供了命令行界面。1.提供与操作系统交互的命令行界面......
  • C10-08-宽字节注入-mysql注入之getshell-sqlmap
    一宽字节注入利用宽字节注入实现“库名-表名”的注入过程。靶场环境:容器镜像:area39/pikachu宽字节概念1、如果一个字符的大小是一个字节的,称为窄字节;2、如果一个字符的大小是两个及以上字节的,称为宽字节;像GB2312、GBK、GB18030、BIG5、Shift_JIS等编码都是常见的宽字节......
  • 【Python爬虫实战】深入解析BeautifulSoup4的强大功能与用法
      ......
  • shell编程小技巧:进程替换
    今天来给大家介绍一个非常好用的shell编程技巧,即进程替换(Processsubstitution)。进程替换可以将一个进程(程序)的输入或输出当做一个文件来使用。它的两种使用形式为:<(cmd)或>(cmd).需要注意的是,<和>与(之间不能有空格!下面通过一个示例来介绍进程替换的具体用法。假如我有一个......
  • 白嫖正版xshell和XFTP
    在哪里可以下载正版免费的xshell和XFTP,并且还能够获得官网免费持久更新白嫖步骤首先直接在浏览器搜索xshell官网点进官网之后直接点击下载接着点击免费授权页面进入之后就可以免费下载了下载安装完成后填写用户名和邮箱并提交,这里就以xshell为例,XFTP同理,这里就不......
  • SHELL
    shell脚本运行4种方法:1.chmoda+xmyshell.sh./myshell.sh2..myshell.sh3.sourcemyshell.sh4./bin/bashmyshell.shshell语法:数据类型:字符串String 变量: 全局变量----环境变量 局部变量----本地变量 变量名=变量值 VAR=10 对比: $变量名......
  • Spacy的一些用法留档(有待更新)
    Spacy的一些用法:#分词nlp=spacy.load("zh_core_web_sm")doc=nlp(u'中国是世界上最大的发展中国家')fortokenindoc:print(token)#另一种分词(不分割特有名词)nlp=spacy.load("zh_core_web_sm")#添加自定义词汇nlp.tokenizer.pkuseg_update_user_dict(......
  • 测开必备-java基础-for循环语句的用法
    什么是for循环?for循环的基本结构什么是for循环?for循环是一种编程中的控制结构,它允许你重复执行一段代码固定的次数。当你需要执行一系列重复的任务时,for循环就非常有用。for循环的基本结构在Java中,for循环的基本结构如下:for (初始化表达式; 循环条件; 步进表达式) {  ......