grep: General Output Control
2.1.3 General Output Control
----------------------------
‘-c’
‘--count’
Suppress normal output; instead print a count of matching lines for
each input file. With the ‘-v’ (‘--invert-match’) option, count
non-matching lines. (‘-c’ is specified by POSIX.)
‘--color[=WHEN]’
‘--colour[=WHEN]’
Surround 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_COLORS’ and 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.
⇒Environment Variables.
WHEN is ‘always’ to use colors, ‘never’ to not use colors, or
‘auto’ to use colors if standard output is associated with a
terminal device and the ‘TERM’ environment variable's value
suggests that the terminal supports colors. Plain ‘--color’ is
treated like ‘--color=auto’; if no ‘--color’ option is given, the
default is ‘--color=never’.
‘-L’
‘--files-without-match’
Suppress normal output; instead print the name of each input file
from which no output would normally have been printed.
‘-l’
‘--files-with-matches’
Suppress 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. (‘-l’ is specified by POSIX.)
‘-m NUM’
‘--max-count=NUM’
Stop after the first NUM selected lines. If NUM is zero, ‘grep’
stops right away without reading input. A NUM of −1 is treated as
infinity and ‘grep’ does not stop; this is the default.
If the input is standard input from a regular file, and NUM
selected lines are output, ‘grep’ ensures 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 ‘grep’ stops after NUM selected lines, it outputs any trailing
context lines. When the ‘-c’ or ‘--count’ option is also used,
‘grep’ does not output a count greater than NUM. When the ‘-v’ or
‘--invert-match’ option is also used, ‘grep’ stops after outputting
NUM non-matching lines.
‘-o’
‘--only-matching’
Print 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 (⇒Other Options).
‘-q’
‘--quiet’
‘--silent’
Quiet; 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 ‘-s’ or ‘--no-messages’ option.
Portability note: Solaris 10 ‘grep’ lacks ‘-q’; portable shell
scripts typically can redirect standard output to ‘/dev/null’
instead of using ‘-q’. (‘-q’ is specified by POSIX.)
‘-s’
‘--no-messages’
Suppress error messages about nonexistent or unreadable files.
(‘-s’ is specified by POSIX.)