Text Processing (Grep/Sed/Awk)
Stop pulling your hair out over multi-line regex matches in sed
đź§© The Challenge
You ever tried to find a config block spanning three lines with sed only to watch it ignore everything after the first newline? I spent way too long trying to chain commands before I realized I was fighting the tool’s fundamental behavior.
đź’ˇ The Fix
Use the N command to pull the next line into the pattern space. It lets you treat a chunk of the file as a single string so you can actually perform replacements across line breaks.
sed -z 's/listen 80\n server_name example.com/listen 443 ssl\n server_name example.com/g' config.conf
⚙️ Why It Works
Passing the -z flag tells sed to treat the entire input as a single line, effectively replacing newline characters with null bytes so they don’t terminate your regex matches prematurely.
🚀 Pro-Tip: Keep a backup file before running this, because if your regex is slightly off, you’ll end up with a mangled config file in one single, unrecoverable line.
Linux Tips & Tricks | © ngelinux.com | 10/8/2026
