Next: , Previous: Matching Control, Up: Command-line Options


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 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_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. The deprecated environment variable GREP_COLOR is still supported, but its setting does not have priority; it defaults to ‘01;31’ (bold red) which only covers the color for matched text. WHEN is ‘never’, ‘always’, or ‘auto’.
-L
--files-without-match
Suppress normal output; instead print the name of each input file from which no output would normally have been printed. The scanning of each file stops on the first match.
-l
--files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning of each file stops on the first match. (-l is specified by POSIX.)
-m num
--max-count=num
Stop reading a file after num matching lines. If the input is standard input from a regular file, and num matching lines are output, grep ensures that the standard input is positioned just after the last matching 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 matching lines, it outputs any trailing context lines. Since context does not include matching lines, grep will stop when it encounters another matching line. 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.
-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. (-q is specified by POSIX.)
-s
--no-messages
Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, 7th Edition Unix grep did not conform to POSIX, because it lacked -q and its -s option behaved like GNU grep's -q option.1 USG-style grep also lacked -q but its -s option behaved like GNU grep's. Portable shell scripts should avoid both -q and -s and should redirect standard and error output to /dev/null instead. (-s is specified by POSIX.)

Footnotes

[1] Of course, 7th Edition Unix predated POSIX by several years!