grep: General Output Control
1
1 2.1.3 General Output Control
1 ----------------------------
1
1 ‘-c’
1 ‘--count’
1 Suppress normal output; instead print a count of matching lines for
1 each input file. With the ‘-v’ (‘--invert-match’) option, count
1 non-matching lines. (‘-c’ is specified by POSIX.)
1
1 ‘--color[=WHEN]’
1 ‘--colour[=WHEN]’
1 Surround the matched (non-empty) strings, matching lines, context
1 lines, file names, line numbers, byte offsets, and separators (for
1 fields and groups of context lines) with escape sequences to
1 display them in color on the terminal. The colors are defined by
1 the environment variable ‘GREP_COLORS’ and default to
1 ‘ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36’ for bold red
1 matched text, magenta file names, green line numbers, green byte
1 offsets, cyan separators, and default terminal colors otherwise.
1 The deprecated environment variable ‘GREP_COLOR’ is still
1 supported, but its setting does not have priority; it defaults to
1 ‘01;31’ (bold red) which only covers the color for matched text.
1 WHEN is ‘never’, ‘always’, or ‘auto’.
1
1 ‘-L’
1 ‘--files-without-match’
1 Suppress normal output; instead print the name of each input file
1 from which no output would normally have been printed. The
1 scanning of each file stops on the first match.
1
1 ‘-l’
1 ‘--files-with-matches’
1 Suppress normal output; instead print the name of each input file
1 from which output would normally have been printed. The scanning
1 of each file stops on the first match. (‘-l’ is specified by
1 POSIX.)
1
1 ‘-m NUM’
1 ‘--max-count=NUM’
1 Stop after the first NUM selected lines. If the input is standard
1 input from a regular file, and NUM selected lines are output,
1 ‘grep’ ensures that the standard input is positioned just after the
1 last selected line before exiting, regardless of the presence of
1 trailing context lines. This enables a calling process to resume a
1 search. For example, the following shell script makes use of it:
1
1 while grep -m 1 PATTERN
1 do
1 echo xxxx
1 done < FILE
1
1 But the following probably will not work because a pipe is not a
1 regular file:
1
1 # This probably will not work.
1 cat FILE |
1 while grep -m 1 PATTERN
1 do
1 echo xxxx
1 done
1
1 When ‘grep’ stops after NUM selected lines, it outputs any trailing
1 context lines. When the ‘-c’ or ‘--count’ option is also used,
1 ‘grep’ does not output a count greater than NUM. When the ‘-v’ or
1 ‘--invert-match’ option is also used, ‘grep’ stops after outputting
1 NUM non-matching lines.
1
1 ‘-o’
1 ‘--only-matching’
1 Print only the matched (non-empty) parts of matching lines, with
1 each such part on a separate output line. Output lines use the
1 same delimiters as input, and delimiters are null bytes if ‘-z’
1 (‘--null-data’) is also used (⇒Other Options).
1
1 ‘-q’
1 ‘--quiet’
1 ‘--silent’
1 Quiet; do not write anything to standard output. Exit immediately
1 with zero status if any match is found, even if an error was
1 detected. Also see the ‘-s’ or ‘--no-messages’ option. (‘-q’ is
1 specified by POSIX.)
1
1 ‘-s’
1 ‘--no-messages’
1 Suppress error messages about nonexistent or unreadable files.
1 Portability note: unlike GNU ‘grep’, 7th Edition Unix ‘grep’ did
1 not conform to POSIX, because it lacked ‘-q’ and its ‘-s’ option
1 behaved like GNU ‘grep’’s ‘-q’ option.(1) USG-style ‘grep’ also
1 lacked ‘-q’ but its ‘-s’ option behaved like GNU ‘grep’’s.
1 Portable shell scripts should avoid both ‘-q’ and ‘-s’ and should
1 redirect standard and error output to ‘/dev/null’ instead. (‘-s’
1 is specified by POSIX.)
1
1 ---------- Footnotes ----------
1
1 (1) Of course, 7th Edition Unix predated POSIX by several years!
1