Don’t just see the log error, see what led up to it
Text Processing (Grep/Sed/Awk)
Don’t just see the log error, see what led up to it
🧩 The Challenge
Ever had an error message pop up in your logs, but it was just a tiny fragment of the problem? You know, the `Segmentation fault` or `HTTP 500` without any context of what called it or what failed before. You end up spending five minutes digging through `less` or `vim` just to scroll up and down, trying to piece together the events that led to that single offending line.
💡 The Fix
There’s a simple `grep` trick that’ll automatically show you the lines *before* and *after* your match. It’s an absolute lifesaver for quickly understanding what went wrong, without the tedious manual scrolling.
grep -C 5 "Segmentation fault" /var/log/syslog
⚙️ Why It Works
The `-C` flag stands for ‘context,’ and that number is how many lines above and below your match `grep` will spit out. You can also use `-A` for ‘after’ and `-B` for ‘before’ if you only care about one direction, or combine them like `-A 3 -B 2`.
🚀 Pro-Tip: Pair this with `zgrep` for those old, rotated log files without needing to uncompress first.
Linux Tips & Tricks | © ngelinux.com | 11/25/2026
