'-q'
'--quiet'
'--silent'
静默模式,立即退出,即使遇到了错误。不写任何内容到标准输出。如果匹配到了内容则退出状态码为0。('-q'是POSIX指定的选项。)
'-s'
'--no-messages'
禁止输出因文件不存在或文件没有读权限而产生的错误信息。('-s'是POSIX指定的选项。)
(注:由于POSIX和GNU grep的差异性,在可移植性的脚本中,应尽量避免使用"-q"和"-s",而是使用重定向的方式重定向到/dev/null)
2.1.4 Output Line Prefix Control(控制输出行的前缀)当输出行有前缀要输出时,它们的顺序总是:文件名、行号、字节的偏移量,这个顺序不会因为前缀控制选项的顺序而改变。
'-b'
'--byte-offset'
Print the 0-based byte offset within the input file before each line of output. If '-o' ('--only-matching') is specified, print the offset of the matching part itself. When 'grep' runs on MS-DOS or MS-Windows, the printed byte offsets depend on whether the '-u' ('--unix-byte-offsets') option is used; see below.
'-H'
'--with-filename'
输出匹配到内容所在文件的文件名。当指定了多个输入文件时,这是默认的。
'-h'
'--no-filename'
禁止输出文件名。当只有一个输入文件时,这是默认的。
'--label=LABEL'
Display input actually coming from standard input as input coming from file LABEL. This is especially useful when implementing tools like 'zgrep'; e.g.:
'-n'
'--line-number'
输出匹配内容在文件中的行号,每个文件都单独从1开始计数。('-n'是POSIX指定的选项。)
'-T'
'--initial-tab'
Make sure that the first character of actual line content lies on a tab stop, so that the alignment of tabs looks normal. This is useful with options that prefix their output to the actual content:'-H', '-n', and '-b'. In order to improve the probability that lines from a single file will all start at the same column, this also causes the line number and byte offset (if present) to be printed in a minimum-size field width.
'-u'
'--unix-byte-offsets'
Report Unix-style byte offsets. This option causes 'grep' to report byte offsets as if the file were a Unix-style text file, i.e., the byte offsets ignore the 'CR' characters that were stripped. This will produce results identical to running 'grep' on a Unix machine. This option has no effect unless the '-b' option is also used; it has no effect on platforms other than MS-DOS and MS-Windows.
'-Z'
'--null'
在输出文件名时,使用"\0"放在文件名后,这会替换原本使用的字符,如换行符或冒号。例如"grep -lZ"输出的每个文件都在同一行而不是分行,"grep -HZ"使得文件名后没有冒号。
无论下面的选项如何设置,grep都不会多次输出同一行。如果指定了"-o"选项,这些选项将失效,并给出一个警告。
'-A NUM'
'--after-context=NUM'
除了输出匹配到的行,还输出匹配到内容的后NUM行。
'-B NUM'
'--before-context=NUM'
除了输出匹配到的行,还输出匹配到内容的前NUM行。
'-C NUM'
'-NUM'
'--context=NUM'
除了输出匹配到的行,还输出匹配到内容的前NUM行和后NUM行。
'--group-separator=STRING'
当使用'-A', '-B' or '-C'时,使用STRING替代默认的组分隔符。
(注:组分隔符表示匹配到的内容的上下文。例如"-A 2",在某行匹配到时,还将输出后两行,这是一个组。下一次匹配成功时,如果是在该组之后行匹配上的,则这两组中间默认使用"--"分隔)
'--no-group-separator'
当使用'-A', '-B' or '-C'时,不输出任何组分隔符,而是将不同组相邻输出。
'-a'
'--text'
Process a binary file as if it were text; this is equivalent to the '--binary-files=text' option.
'--binary-files=TYPE'
If the first few bytes of a file indicate that the file contains binary data, assume that the file is of type TYPE. By default, TYPE is 'binary', and 'grep' normally outputs either a one-line message saying that a binary file matches, or no message if there is no match. If TYPE is 'without-match', 'grep' assumes that a binary file does not match; this is equivalent to the '-I' option. If TYPE is 'text', 'grep' processes a binary file as if it were text; this is equivalent to the '-a' option. Warning: '--binary-files=text' might output binary garbage, which can have nasty side effects if the output is a terminal and if the terminal driver interprets some of it as commands.