shell技能树---grep详解

 目录

1. 功能介绍

2. 语法格式

2.1 Pattern selection and interpretation

2.1.1    -E, --extended-regexp

2.1.1.1   字符匹配   

2.1.1.2   次数匹配 

2.1.1.3  位置锚定

2.1.1.4  引用

2.1.2    -F, --fixed-strings

2.1.3    -G, --basic-regexp

2.1.4    -P, --perl-regexp

2.1.5    -e, --regexp=PATTERNS

2.1.6    -f, --file=FILE

2.1.7    -i, --ignore-case

2.1.8    --no-ignore-case

2.1.9    -w, --word-regexp

2.1.10    -x, --line-regexp

2.1.11    -z, --null-data

2.2 Miscellaneous

2.2.1    -s, --no-messages

2.2.2    -v, --invert-match 

2.2.3    -V, --version

2.2.4    --help

2.3 Output control

2.3.1    -m, --max-count=NUM

2.3.2    -b, --byte-offset

2.3.3    -n, --line-number

2.3.4    --line-buffered

2.3.5    -H, --with-filename

2.3.6    -h, --no-filename

2.3.7    --label=LABEL

2.3.8    -o, --only-matching

2.3.9    -q, --quiet, --silent

2.3.10    --binary-files=TYPE

2.3.11    -a, --text

2.3.12    -I

2.3.13    -d, --directories=ACTION

2.3.14    -D, --devices=ACTION

2.3.15    -r, --recursive

2.3.16    -R, --dereference-recursive

2.3.17    --include=GLOB

2.3.18    --exclude=GLOB

2.3.19    --exclude-from=FILE

2.3.20    --exclude-dir=GLOB

2.3.21    -L, --files-without-match

2.3.22    -l, --files-with-matches

2.3.23    -c, --count

2.3.24    -T, --initial-tab

2.3.25    -Z, --null

2.4 Context control

2.4.1    -B, --before-context=NUM

2.4.2    -A, --after-context=NUM

2.4.3    -C, --context=NUM

2.4.4    -NUM

2.4.5    --group-separator=SEP

2.4.6    --no-group-separator

2.4.7    --color[=WHEN],--colour[=WHEN]

2.4.8    -U, --binary

2.5 Others

2.5.1 Exit status

2.5.2 FILES

2.5.3 multiple patterns


1. 功能介绍

Search for PATTERNS in each FILE.

在每个文件中搜索匹配的模式的行,然后输出到标准输出。

本文主要是对 grep --help的帮助文件的详细解释。

2. 语法格式

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

2.1 Pattern selection and interpretation

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

2.1.1    -E, --extended-regexp

     PATTERNS are extended regular expressions
     扩展的正则表达式。扩展主要是针对基础表达式而言的。两者差异参照下表。

基础正则表达式扩展正则表达式
字符匹配相同相同
次数匹配除了*,其余需增加\。比如\?,\{m,n\}
位置锚定相同相同
分组括号或者|需要加\
引用相同相同

         参考: 【153】基本正则表达式(grep)和拓展正则表达式(egrep)-CSDN博客

2.1.1.1   字符匹配   

2.1.1.2   次数匹配 

2.1.1.3  位置锚定

行锚定(行首,行尾)

单词(词首,词尾) 注:不包含特殊字符的连续字符组成的串叫单词
​2.1.1.3  分组

2.1.1.4  引用

2.1.2    -F, --fixed-strings

    PATTERNS are strings。字符串不进行转义。

2.1.3    -G, --basic-regexp

     PATTERNS are basic regular expressions
     基础表达式是默认的正则表达式。参照 “2.1.1    -E, --extended-regexp”章节。

2.1.4    -P, --perl-regexp

    PATTERNS are Perl regular expressions
    略(接触比较少,先省略)

2.1.5    -e, --regexp=PATTERNS

    use PATTERNS for matching   

2.1.6    -f, --file=FILE

     take PATTERNS from FILE

2.1.7    -i, --ignore-case

     ignore case distinctions in patterns and data  

2.1.8    --no-ignore-case

     do not ignore case distinctions (default),-i 和 --no-ignore-case共存时,后来的优先。

2.1.9    -w, --word-regexp

     match only whole words
     仅匹配整个单词(单词是由字母,数字和下滑线组成,其余为单词的分隔符)  
 

2.1.10    -x, --line-regexp

     match only whole lines
     仅匹配整行

2.1.11    -z, --null-data

    a data line ends in 0 byte, not newline 使用\0作为换行符

2.2 Miscellaneous

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

2.2.1    -s, --no-messages

suppress error messages 不输出错误信息

2.2.2    -v, --invert-match 

 select non-matching lines 选择没有被匹配的行 

2.2.3    -V, --version

display version information and exit 显示版本信息,然后退出

2.2.4    --help

display this help text and exit 显示帮助文件,然后退出

2.3 Output control

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

2.3.1    -m, --max-count=NUM

stop after NUM selected lines 匹配的最大行数(可以理解为只输出匹配的前NUM个)

2.3.2    -b, --byte-offset

print the byte offset with output lines(输出在搜索文档中的字节的位置,注意从0开始和换行符计数)

2.3.3    -n, --line-number

print line number with output lines

2.3.4    --line-buffered

flush output on every line  对每一行都强制输出。参照以下文章,对于检测实时输出的比如tail,如果不使用--line-buffered,默认是块缓冲(默认的大小为4096 bytes,因系统和配置而异)

解决 Grep 的多次管道过滤问题-CSDN博客

2.3.5    -H, --with-filename

print file name with output lines, 搜索单个文件默认不输出文件名,可以使用-H强制输出文件名。

2.3.6    -h, --no-filename

suppress the file name prefix on output 不输出文件,默认的

2.3.7    --label=LABEL

use LABEL as the standard input file name prefix

2.3.8    -o, --only-matching

 show only nonempty parts of lines that match

2.3.9    -q, --quiet, --silent

suppress all normal output 不进行输出。

2.3.10    --binary-files=TYPE

assume that binary files are TYPE; TYPE is 'binary', 'text', or 'without-match'
对于普通文件不起作用。

2.3.11    -a, --text

equivalent to --binary-files=text

2.3.12    -I

equivalent to --binary-files=without-match

2.3.13    -d, --directories=ACTION

 how to handle directories; ACTION is 'read', 'recurse', or 'skip'

2.3.14    -D, --devices=ACTION

how to handle devices, FIFOs and sockets; ACTION is 'read' or 'skip'

TODO

2.3.15    -r, --recursive

like --directories=recurse

2.3.16    -R, --dereference-recursive

likewise, but follow all symlinks 取消引用相同的递归

2.3.17    --include=GLOB

search only files that match GLOB (a file pattern)

2.3.18    --exclude=GLOB

skip files that match GLOB

2.3.19    --exclude-from=FILE

skip files that match any file pattern from FILE

2.3.20    --exclude-dir=GLOB

skip directories that match GLOB
正则表达式TODO

2.3.21    -L, --files-without-match

print only names of FILEs with no selected lines

2.3.22    -l, --files-with-matches

print only names of FILEs with selected lines

参照“2.3.21    -L, --files-without-match”章节。

2.3.23    -c, --count

print only a count of selected lines per FILE

2.3.24    -T, --initial-tab

make tabs line up (if needed)

2.3.25    -Z, --null

print 0 byte after FILE name

2.4 Context control

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  print SEP on line between matches with context
      --no-group-separator  do not print separator for matches with context
      --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)

2.4.1    -B, --before-context=NUM

print NUM lines of leading context

2.4.2    -A, --after-context=NUM

print NUM lines of trailing context

参照 “2.4.1    -B, --before-context=NUM”

2.4.3    -C, --context=NUM

print NUM lines of output context

参照 “2.4.1    -B, --before-context=NUM”

2.4.4    -NUM

same as --context=NUM

参照 “2.4.1    -B, --before-context=NUM”

2.4.5    --group-separator=SEP

print SEP on line between matches with context

2.4.6    --no-group-separator

do not print separator for matches with context
参照 “2.4.5    --group-separator=SEP”章节
 

2.4.7    --color[=WHEN],--colour[=WHEN]

use markers to highlight the matching strings; WHEN is 'always', 'never', or 'auto'

2.4.8    -U, --binary

 do not strip CR characters at EOL (MSDOS/Windows)
TODO
 

2.5 Others

Exit status is 0 if any line is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.

When FILE is '-', read standard input.  With no FILE, read '.' if
recursive, '-' otherwise.  With fewer than two FILEs, assume -h.


PATTERNS can contain multiple patterns separated by newlines.

2.5.1 Exit status

Exit status is 0 if any line is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.


以下是两种对Exit status的应用场景

renhl@verygood:~/usb/000$ cat 1.txt
a 1
b 2
c 3
b 4
e 5
b 6
g 7
renhl@verygood:~/usb/000$ grep -q "b" 1.txt; if [ $? -eq 0 ]; then echo "Match found!"; else echo "Match not found!"; fi
Match found!
renhl@verygood:~/usb/000$ grep -q "h" 1.txt; if [ $? -eq 0 ]; then echo "Match found!"; else echo "Match not found!"; fi
Match not found!
renhl@verygood:~/usb/000$ if grep -q "b" 1.txt; then echo "Match found!"; else echo "Match not found!"; fi
Match found!
renhl@verygood:~/usb/000$ if grep -q "h" 1.txt; then echo "Match found!"; else echo "Match not found!"; fi
Match not found!
renhl@verygood:~/usb/000$

if any error occurs and -q is not given, the exit status is 2. 

2.5.2 FILES

When FILE is '-', read standard input.  With no FILE, read '.' if recursive, '-' otherwise.  With fewer than two FILEs, assume -h.

当FILE是-时,从标准输入读取;当没有指定FILE时,如果recursive,从当前目录.;否则从标准输入读取。 

2.5.3 multiple patterns

PATTERNS can contain multiple patterns separated by newlines.

  • 53
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值