grep [options] pattern [file...]
grep [options] [-e pattern | -f patternFile] [file…]
Searches the input
(or STDIN if no files are
named, or if
file is - )
for lines containing a match to the
-G --basic-regexp pattern is a Basic Regular Expression (default).
) must be preceeded by
\ to ENABLE special meaning
-E --extended-regexp pattern is an Extended Regular Expression.
egrep is the same as
-F --fixed-strings pattern is a list of fixed strings, separated by newlines, any of which is to be matched.
fgrep is the same as
-P --perl-regexp Interpret
pattern as a Perl regular expression.
| controling matching
| Invert the sense of matching to |
select non-matching lines.
pattern as the
useful to protect
patterns beginning with
- from being intrepreted as options.
| Obtain patterns from |
file, one per line. An empty file contains zero patterns and matches nothing.
matches must form whole words. |
The matching substring must either be at the beginning of the line, or preceded by a
non-alphanumeric character and
must be either at the end of the line or followed by a non-alphanumeric character.
Alphanumeric characters are letters, digits, and the underscore.
| exactly match the whole line.
|Obsolete synonym for |
| controlling output
| only the matching part of the line is output.
when may be
The matched sections can be displayed in color as per
$GREP_COLOR in the form
ff is the code used for the foreground and
bb is used for the background.
magenta on yellow 35;43
blue on cyan 34;46
red 31 (default)
Foreground: black 30, green 32, yellow
33, blue 34, magenta 35,
cyan 36, white 37, red 31 (default)
Background: black 40, green 42, yellow
43, blue 44, magenta 45,
underscore 4, [blink] 5, inverse 7
Codes are not output when inappropriate (For example: to a file or pipe) unless
grep --color=always birds logfile | more -r
num lines of context before and after match
num lines of context before lines containing the
then the lines containing
num lines of context after outputting the
Places a line containing
-- between sets of matches.
| || grep -C1 day
|| grep -B2 day
|| grep -A2 day
| input || output
| week || ||
| tuesday || ||
| sunday ||
| week2 || ||
| f ||
|| || f
|only output count of matches (or |
-cv non-matches) for each input file
Prefixed by '
filename :' if multiple files, even if count is 0
grep -c images *html |grep -v 0$ #dont show lines with 0 count
| Prefix each line with the line number
|prefix each line with the byte offset within the input file
|Use Unix-style byte offsets. |
report byte offsets i.e. with CR characters stripped off.
only for MS-DOS and MS-Windows.
| Quiet; do not write to |
Errors like "
Permission denied" are written to
STDERR and return code is set to 2.
Exit with zero status as soon as a match is found.
If no match is found
$? is set to 1.
|Suppress error messages about nonexistent or unaccessable files normally sent to |
| filename is prepended to each match when multiple files are searched.|
| outputs the only the filename of files containing a match.|
Once a match is found proceeds to the next file.
|outputs the filenname with no match
| terminate filename with |
Used when file names contain newlines!.
Used with commands
Displays input actually coming from standard input as input coming from file |
label. Useful for tools like
gzip -cd foo.gz |grep --label=foo something
| stop after |
num matching lines.
If the input is standard input from a regular file, and
num matching lines are output, the standard input is positioned to
just after the last matching line, enables calling process to resume a search.
> while grep -B 2 -m 1 bod; do echo ; done