General Output Control (GNU Grep 3.7)
Next: Output Line Prefix Control, Previous: Matching Control, Up: Command-line Options [Contents][Index]
2.1.3 General Output Control
-c¶
--countSuppress normal output; instead print a count of matching lines for each input file. With the
-v(--invert-match) option, count non-matching lines. (-cis specified by POSIX.)--color[=WHEN]¶
--colour[=WHEN]Surround the matched (non-empty) strings, matching lines, context lines, file names, line numbers, byte offsets, and separators (for fields and groups of context lines) with escape sequences to display them in color on the terminal. The colors are defined by the environment variable
GREP_COLORSand default to ‘ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36’ for bold red matched text, magenta file names, green line numbers, green byte offsets, cyan separators, and default terminal colors otherwise. The deprecated environment variableGREP_COLORis still supported, but its setting does not have priority; it defaults to ‘01;31’ (bold red) which only covers the color for matched text.WHENis ‘never’, ‘always’, or ‘auto’.-L¶
--files-without-matchSuppress normal output; instead print the name of each input file from which no output would normally have been printed.
-l¶
--files-with-matchesSuppress normal output; instead print the name of each input file from which output would normally have been printed. Scanning each input file stops upon first match. (
-lis specified by POSIX.)-m num¶
--max-count=numStop after the first
numselected lines. If the input is standard input from a regular file, andnumselected lines are output,grepensures that the standard input is positioned just after the last selected line before exiting, regardless of the presence of trailing context lines. This enables a calling process to resume a search. For example, the following shell script makes use of it:while grep -m 1 'PATTERN' do echo xxxx done < FILE
But the following probably will not work because a pipe is not a regular file:
# This probably will not work. cat FILE | while grep -m 1 'PATTERN' do echo xxxx done
When
grepstops afternumselected lines, it outputs any trailing context lines. When the-cor--countoption is also used,grepdoes not output a count greater thannum. When the-vor--invert-matchoption is also used,grepstops after outputtingnumnon-matching lines.-o¶
--only-matchingPrint only the matched (non-empty) parts of matching lines, with each such part on a separate output line. Output lines use the same delimiters as input, and delimiters are null bytes if
-z(--null-data) is also used (see Other Options).-q¶
--quiet
--silentQuiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. Also see the
-sor--no-messagesoption. (-qis specified by POSIX.)-s¶
--no-messagesSuppress error messages about nonexistent or unreadable files. Portability note: unlike GNU
grep, 7th Edition Unixgrepdid not conform to POSIX, because it lacked-qand its-soption behaved like GNUgrep’s-qoption.1 USG-stylegrepalso lacked-qbut its-soption behaved like GNUgrep’s. Portable shell scripts should avoid both-qand-sand should redirect standard and error output to/dev/nullinstead. (-sis specified by POSIX.)
Footnotes
(1)
Of course, 7th Edition Unix predated POSIX by several years!
Next: Output Line Prefix Control, Previous: Matching Control, Up: Command-line Options [Contents][Index]