Tools Practical By Samson Tanimawo, PhD Published May 12, 2026 4 min read

grep With Context: -A and -B

grep -A and -B for log investigation.

Flags

-A N: N lines after match. -B N: N lines before. -C N: both.

Reads logs better than tail | head.

Examples

grep -B5 -A5 ERROR app.log shows context around errors.

grep -B0 -A20 'starting up' shows startup sequence.

Limit

For huge files, prefer ripgrep (rg).

Faster on multi-GB logs.