grep 强大的文本搜索工具

知道grep

grep英文全称 “global search regular expression(RE) and print out the line
中文翻译为“全面搜索正则表达式并把行打印出来

grep是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。用于过滤/搜索的特定字符。可使用正则表达式能配合多种命令使用,使用上十分灵活。

如果不指定任何文件名称,或是所给予的文件名为 -,则 grep 指令会从标准输入设备读取数据。

怎么用grep

Usage: grep [OPTION]... PATTERN [FILE]...

参数分类

Regexp selection and interpretation(正则表达式选择和解释)

  -E, --extended-regexp     PATTERN是一个扩展正则表达式(ERE)
  -F, --fixed-strings       PATTERN是一组以换行符分隔的固定字符串
  -G, --basic-regexp        PATTERN是一个基本正则表达式(BRE)
  -P, --perl-regexp         PATTERN是一个Perl正则表达式
  -e, --regexp=PATTERN      PATTERN用于匹配
  -f, --file=FILE           从文本文件中获取PATTERN
  -i, --ignore-case         忽略大小写
  -w, --word-regexp         强制PATTERN只匹配整个单词
  -x, --line-regexp         强制PATTERN只匹配一整行
  -z, --null-data           a data line ends in 0 byte, not newline

grep -E,与egrep命令是相同的,grep -F,与fgrep命令是相同的。

命令使用

grep

Miscellaneous(杂七杂八选项)

  -s, --no-messages        不输出错误消息
  -v, --invert-match       匹配不匹配的行
  -V, --version             显示版本号码,并退出
      --help                显示帮助信息,并退出

命令使用

grep

Output control(输出控制)

  -m, --max-count=NUM       stop after NUM matches
  -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 the file name for each match
  -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 the part of a line matching PATTERN
  -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=FILE_PATTERN
                            search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN
                            skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN directories that match PATTERN will be skipped.
  -L, --files-without-match print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count               print only a count of matching lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

命令使用

grep

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
      --group-separator=SEP use SEP as a group separator
      --no-group-separator  use empty string as a group separator
      --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)
  -u, --unix-byte-offsets   report offsets as if CRs were not there
                            (MSDOS/Windows)

命令使用

grep

未完待补充……

猜你喜欢

转载自blog.csdn.net/Tom942067059/article/details/122245271