Lesson 10 of 13 · 20m
Matching Across Lines: Records That Aren't Lines
When records span lines: awk paragraph mode, slurp flags, rg -U, the m-versus-s distinction, and the bounded-lazy idiom for folded text.
grep can't see a stack trace. The exception you need is forty indented lines hanging off one timestamped line, and every line-oriented tool will happily hand you the header and drop the Caused by: that actually names the bug. The fix isn't a cleverer pattern — it's changing what counts as a record.
Paragraph mode: awk's RS=""
Set the record separator to the empty string and blank lines delimit records instead of newlines. Pair it with FS="\n" and each line inside a paragraph becomes a field:
# Print the first line of every paragraph that mentions OOM
awk -v RS="" -F'\n' '/OutOfMemoryError/ { print $1 }' app.logJava logs, mail headers, and -----BEGIN CERTIFICATE----- blocks all collapse into this shape. It's the cheapest multiline tool because nothing gets slurped — awk still streams.
The inverse exists too: in GNU awk, RS can be a regex, so RS="^2025" treats every line starting with a year as a new record boundary. It's gawk-only and it splits on the separator, not the start of the next record — subtleties like that are why the bounded-lazy idiom below is usually the better tool inside real regex engines.
Slurp the whole file
When records aren't blank-line separated, read everything as one string and let . cross newlines:
perl -0777 -ne 'print "$&\n" while m/BEGIN CERTIFICATE.*?END CERTIFICATE/gs' bundle.pem-0777 disables the record separator entirely; s (dotall) makes . eat \n; g in while m//g iterates every match. GNU tools have a cousin: grep -z and sed -z use NUL as the record separator, so sed -z 's/foo\nbar/baz/' edits across a line break. GNU-only — BSD sed has no -z.
ripgrep does multiline without you slurping anything:
rg -U 'location ~ \^/api \{\n\s+proxy_pass' nginx.conf # -U = --multiline
rg -U --multiline-dotall 'BEGIN.*?END' bundle.pem # dot crosses newlinesm and s solve different problems
schanges what.consumes: dotall mode eats newlines.mchanges what^and$assert: line edges instead of input edges.
import re
log = "2025-03-14 INFO boot\n at com.app.Main.run(Main.java:42)\n at com.app.Main.main(Main.java:10)\n2025-03-14 INFO ready"
stack = re.findall(r"^\s+at .*(?:\n\s+at .*)*", log, re.M)
print(stack) # [' at com.app.Main.run(Main.java:42)\n at com.app.Main.main(Main.java:10)']m alone doesn't let . span lines; s alone doesn't make ^ mean "start of a line." Multiline work often wants both — for instance re.findall(r"^(?P<ts>\S+ \S+) (?P<rest>.*?)(?=^\S|\Z)", log, re.M | re.S) splits a timestamped log into records by "until the next flush-left line."
The bounded-lazy idiom for folded records
"Until the next line that starts flush-left" is the correct shape for tracebacks and folded headers:
re.findall(r"Traceback.*?(?=\n\S|\Z)", log, re.S)Lazy .*? bounded by a lookahead — not greedy .*, which eats to the end of the file and hands you one giant match containing everything.
Python's str.splitlines() handles \r\n, \r, \v, \f, and Unicode line boundaries (\u2028, \u2029) — worth knowing before you split on \n by hand. When you need the endings kept, splitlines(keepends=True) preserves them.
Know when to leave lines
CSV quoted fields can contain raw newlines — "notes","line one\nline two". You can't paragraph-mode your way out of that: RFC 4180 says a record ends at a newline outside quotes, which is exactly the state machine a real CSV parser maintains. The when-not-to-regex lesson covers the rest.
Field notes
pcre2grep -Mdoes true multiline matching on streams.- Slurping a 3 GB log into one string is a choice.
awkparagraph mode andrg -Ustream;read()+findalldoes not. - PEM blocks,
-----BEGIN ...-----sentinels, and YAML folded scalars all match cleanly with a lazy bounded span unders. re.finditeroverre.Mbeatsfindallwhen you want to stop at the first good match instead of materializing every one.
Keep reading
Related articles from the SudoSecurity blog.
Java Pulled Primitive Patterns After Five Previews. JDK 28 Won't Compile Them.
Primitive types in patterns, instanceof, and switch previewed in every JDK from 23 to 27. On November 9 the Amber team withdrew the feature, and it won't be in JDK 28 even with --enable-preview. Here's why Valhalla changed the plan, what replaces it, and how to rewrite code that used it.
.NET 11 Is Out. Union Types Got the Applause, but unsafe Is What Changed.
.NET 11 shipped on November 10, 2026, and C# 15 finally has union types, the feature people have asked for since pattern matching arrived.
PHP 8.6 Finally Turns On the Session Settings You Were Told to Set in 2006
Every PHP hardening guide for two decades has told you to flip the same three session settings. PHP 8.6, due November 19, 2026, flips them for you: strict mode on, HttpOnly on, SameSite set to Lax.
Rust 1.100: The Never Type Is Finally Stable, and Cargo Learned to Wait
Rust 1.100 stabilizes the never type after ten years and five failed attempts, along with the core Allocator API and a DropGuard helper. The change most teams will notice is in Cargo: a one-line config that makes it skip any crate version published in the last few days.