grep

grep for a string, include lines before and after

cat FILE | grep 'FINDTHIS' -B NUM-LINES-BEFORE -A NUM-LINES-AFTER

Read lines from file A as patterns to match in file B

grep -F -f a.txt b.txt

or patterns that don’t match

grep -Fv -f a.txt b.txt

-F treats the strings as “fixed” with literal interpretations of characters like .

Example

└─$ cat alpha.txt
 
a
b
c
d
e
f
g
h
i
 
 
└─$ cat vowel.txt
a
e
i
 
 
└─$ grep -F -f  vowel.txt alpha.txt
a
e
i
 
 
└─$ grep -Fv -f  vowel.txt alpha.txt
b
c
d
f
g
h

Search recursively through a folder

grep -rniI --exclude-dir=.git 'pattern' /path/to/search

-r recurse, -n line numbers, -i ignore case, -I skip binary files.

Limit to certain files, or just list which files match:

grep -rn --include='*.md' 'pattern' .
grep -rl 'pattern' .

See also