Place Holder Projects Code
Bash MySQL JavaScript MySQL Quick Reference
Notes Documents Return of the Fed Login

Show lines surrounding match with grep

It can sometimes be useful to have grep return the lines that come directly before or after a match.  This can be easily achieved in the GNU version of grep.

In addition to being recursive and showing the line number, the following shows 2 lines before each match, and 4 lines after.

grep -rn . -A 4 -B 2 -e pattern

Here -A and -B represent lines after and lines before.  The same number can be used for both with the -C switch.