Text Processing (Grep/Sed/Awk)
Stop letting multi-line log entries ruin your grep game
🧩 The Challenge
Dealing with Java stack traces or JSON blobs that span ten lines is a nightmare when you just want to find a single error. You end up grepping for the error code and losing the context of what actually crashed.
💡 The Fix
Use sed to treat the whole block as a single searchable unit by pulling the lines together based on their timestamps. It saves you from having to scroll through pages of noise just to see the full trace.
sed -n '/^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}/H; /^[^0-9]/H; /^[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}/x; $g; s/\n/ /g; /ERROR/p' app.log
⚙️ Why It Works
This approach uses the hold space to accumulate lines until it hits the next timestamp, effectively flattening the file so your search patterns hit the entire message at once. It’s way faster than trying to use context flags on files with varying line counts.
🚀 Pro-Tip: Pipe that output into fold -s if your terminal starts wrapping everything into an unreadable mess.
Linux Tips & Tricks | © ngelinux.com | 10/5/2026
