grep命令中文手册(info grep翻译)(3)

'-D ACTION'
'--devices=ACTION'
If an input file is a device, FIFO, or socket, use ACTION to process it. By default, ACTION is 'read', which means that devices are read just as if they were ordinary files. If ACTION is 'skip', devices, FIFOs, and sockets are silently skipped.

'-d ACTION'
'--directories=ACTION'
If an input file is a directory, use ACTION to process it. By default, ACTION is 'read', which means that directories are read just as if they were ordinary files (some operating systems and file systems disallow this, and will cause 'grep' to print error messages for every directory or silently skip them). If ACTION is 'skip', directories are silently skipped. If ACTION is 'recurse', 'grep' reads all files under each directory, recursively; this is equivalent to the '-r' option.

'--exclude=GLOB'
忽略basename能被GLOB匹配到的文件。GLOB通配符包括:"*"、"?"和"[...]"。

'--exclude-from=FILE'
从FILE中读取exclude的排除规则。

'--exclude-dir=DIR'
筛选出不进行递归搜索的目录,使用DIR进行匹配。

'-I'
Process a binary file as if it did not contain matching data; this is equivalent to the '--binary-files=without-match' option.

'--include=GLOB'
只搜索basename能被GLOB匹配的文件。

'-r'
'-R'
'--recursive'
从命令行中给定的目录中递归进去,搜索其中的每个文件和目录。

2.1.7 Other Options(其他选项)

'--line-buffered'
Use line buffering on output. This can cause a performance penalty.

'--mmap'
This option is ignored for backwards compatibility. It used to read input with the 'mmap' system call, instead of the default 'read' system call. On modern systems, '--mmap' rarely if ever yields better performance.

'-U'
'--binary'
Treat the file(s) as binary. By default, under MS-DOS and MS-Windows, 'grep' guesses the file type by looking at the contents of the first 32kB read from the file. If 'grep' decides the file is a text file, it strips the 'CR' characters from the original file contents (to make regular expressions with '^' and '$' work correctly). Specifying '-U' overrules this guesswork, causing all files to be read and passed to the matching mechanism verbatim; if the file is a text file with 'CR/LF' pairs at the end of each line, this will cause some regular expressions to fail. This option has no effect on platforms other than MS-DOS and MS-Windows.

'-z'
'--null-data'
以"\0"作为输入行的分隔符,而不再以换行符分隔两行。
(注:这为grep提供了简单的跨行匹配的能力。见后文示例14。)

2.2 Exit Status(退出状态码)

通常情况下,如果能匹配到内容,则退出状态码为0,否则为1。但是如果发生了错误,则退出状态码为2,除非使用了"-s"或"-q"选项。

2.3 'grep' Programs(各种grep程序)

有4种grep程序分别支持不同的搜索引擎,使用下面4个选项可以选择使用哪种grep程序。

'-G'
'--basic-regexp' 使用基础正则表达式引擎解析PATTERN,因此只支持基础正则表达式(BRE)。这是默认grep程序。

'-E'
'--extended-regexp'
使用扩展正则表达式引擎解析PATTERN,因此支持扩展正则表达式(ERE)。('-E'是POSIX指定的选项。)

'-F'
'--fixed-strings'
不识别正则表达式,而是使用字符的字面意义解析PATTERN,因此只支持固定字符串的精确匹配。('-F'是POSIX指定的选项。)

'-P'
'--perl-regexp'
使用perl正则表达式引擎解析PATTERN,因此支持Perl正则表达式。但该程序正处于研究测试阶段,因此会给出一个警告。

此外,"grep -E"和"grep -F"可分别简写为egrep和fgrep。但这两个简写程序是传统写法,已被废弃,虽仍支持,但只是为了兼容老版本程序。
(注:还有zgrep和pgrep,但它们不是grep家族的程序,zgrep是gzip提供,pgrep用于查看进程名和pid的映射关系)

3 Regular Expressions(正则表达式)

正则表达式是一种用于描述字符串集合的表达式。正则表达式类似于算术表达式,也使用各种操作符组合各短小表达式。grep可以理解三种不同版本的正则表达式:基础正则表达式BRE、扩展正则表达式ERE和Perl正则表达式。下面所描述的是扩展正则表达式的内容,在后文会比较BRE和ERE的不同之处。而Perl正则功能更完整、性能更好,可以从pcresyntax(3)和pcrepattern(3)中获取详细信息,但有些操作系统中可能无法获取。

3.1 Fundamental Structure(基本结构)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/13491.html