sed & awk
sedawkunixtext-processingmultilinepattern-space
Abstraction: Advanced sed multiline pattern space commands N, P, D for complex text transformations
Key points:
- Three multiline commands: N (append next input line to pattern space), P (print first line of pattern space), D (delete first line and restart)
- N differs from n: lowercase n outputs current pattern space before reading next line; N appends without outputting
- Match embedded newlines with
\nin regex; insert newline in replacement using backslash followed by literal newline (not\n) - Classic use case: match a phrase split across two lines by reading next line into pattern space then substituting across the embedded newline
- D command removes only the first line of pattern space and passes remainder back to top of script, enabling a sliding-window processing loop
- N/P/D combination: output current line (P), remove it (D), next iteration processes formerly-second line as current
Connections: Sed · Text Processing · Unix Tools
Source: http://docstore.mik.ua/orelly/unix/sedawk/ch06_01.htm