Grep Usage:

Print all the matches for a Pattern:

grep “[Pattern]” [/path/to/file.txt]

Print Lines + Matches for a Pattern:

grep -n “[Pattern]” [/path/to/file.txt]

Print all the matches that are NOT a Pattern:

grep -v “[Pattern]” [/path/to/file.txt]

Display number of matches:

grep -c “[Pattern]” [/path/to/file.txt]

Print Filenames of files that contain the pattern in it:

grep -l “[Pattern]” [/dir/to/check/files/]

Print all Matches of Pattern regardless of case:

grep -i “[PATTERN]” [/path/to/file.txt]

Print only exact maches of the Pattern:

grep -x “[Pattern]” [/path/to/file.txt]

Specify a file to use as a Pattern:

grep -f [/path/to/pattern/file.txt] [/path/to/file]

Use RegEx (Print anything that matches with an e at the end):

grep “e$” [/path/to/file.txt]

Use Egrep for logical operators(or):

egrep [Pattern1|Pattern2] [/path/to/file.txt]

Show color with grep:

grep --color=auto

Fast grep with fgrep (no RegEx):

fgrep “[Pattern$]” [/path/to/file.txt]

Display N Lines Before or N Lines After or N Lines Both Before & After:

grep -A[N] “[Pattern]” [/path/to/file.txt]
grep -B[N] “[Pattern]” [/path/to/file.txt]
grep -C[N] “[Pattern]” [/path/to/file.txt]