Site icon New Generation Enterprise Linux

Stop wrestling with multi-line JSON logs in sed

Text Processing (Grep/Sed/Awk)

Stop wrestling with multi-line JSON logs in sed

🧩 The Challenge

Dealing with application logs that spill over multiple lines because of a stack trace is an absolute nightmare when you’re just trying to grep for a specific request ID. I’ve wasted so many afternoons trying to pipe standard logs into grep only to lose all the context for the actual error.

💡 The Fix

Use sed’s range addressing feature to grab the block of text starting at the line with your ID and ending at the next predictable delimiter. It turns a wall of noise into a readable chunk.

sed -n '/ID-12345/,/---/p' application.log

⚙️ Why It Works

Setting the -n flag keeps sed from printing every single line, and the syntax /start/,/end/p forces it to buffer and output only the exact block you need. It treats the log file like a collection of episodes rather than a stream of consciousness.

🚀 Pro-Tip: If your log delimiter is something weird like a newline character, switch to perl -0777 -ne to slurp the whole file into memory before matching.

Linux Tips & Tricks | © ngelinux.com | 10/21/2026

0 0 votes
Article Rating
Exit mobile version