Reading Ahead in the Input Stream
sed normally works one line at a time, but many tasks require looking at more than the current line. The 'n' and 'N' commands both fetch the next input line, yet they behave very differently. 'n' prints the current pattern space (unless auto-print is off), then replaces it with the next line. 'N' instead appends the next line to the current pattern space, joining them with an embedded newline, so both lines are available together. Choosing between them is the key to windowed, multi-line edits.
Cricket analogy: 'n' is like finishing your shot and facing the next delivery fresh, while 'N' is like a batting partnership where both batsmen are at the crease together and you plan the over as a pair.
The Lowercase 'n' Command
When sed reaches 'n', it first outputs the current pattern space if auto-print is enabled, then reads the next line into the pattern space and continues with the commands that follow 'n'. Crucially it does NOT restart the script from the top — execution resumes at the command after 'n'. This makes 'n' ideal for 'act on every second line' patterns, where you print or skip the current line and then operate on the one after it.
Cricket analogy: 'n' is like a strike rotation single: the current batsman completes the run (prints), the new batsman takes strike (next line loads), and play continues from that ball, not the start of the innings.
The Uppercase 'N' Command
'N' appends a newline plus the next input line to the pattern space without printing, so after 'N' the pattern space holds two lines separated by an embedded '\n'. You can then match across that newline — for example joining a header with its value or matching a pattern that spans two lines. A subtle detail: in POSIX sed, if 'N' is executed on the last line with no next line to fetch, it prints the pattern space and ends; GNU sed instead keeps the pattern space and proceeds so no data is lost.
Cricket analogy: 'N' is like reviewing two consecutive deliveries on the big screen together to judge a run-out, seeing both frames stitched in one clip.
Joining Pairs of Lines
# Join every pair of lines with a space instead of a newline.
printf 'name\nAlice\ncity\nParis\n' | sed 'N; s/\n/: /'
# Output:
# name: Alice
# city: Paris
# Delete a line and the line that follows a match using N.
sed '/START/{ N; d }' file.txtAfter 'N', the embedded newline between the two lines can be matched with '\n' in a substitution. That is how you convert two physical lines into one logical line: 'N; s/\n/ /' replaces the joining newline with a space.
Beware of 'N' on the final line. In strict POSIX behaviour, an 'N' with no next line to read causes sed to print the current pattern space and quit, which can silently drop your intended edit on the last record. GNU sed avoids this, but for portable scripts add a guard like '$!N' to run N only when NOT on the last line.
- 'n' prints the current pattern space (if auto-print is on) then loads the next line, resuming after the 'n'.
- 'N' appends the next line to the pattern space with an embedded newline, without printing.
- Neither command restarts the script; execution continues at the following command.
- After 'N' you can match across lines using '\n' in the pattern.
- '$!N' runs N only when not on the last line, guarding against POSIX last-line behaviour.
- 'n' suits every-other-line tasks; 'N' suits multi-line joins and cross-line matching.
- GNU sed preserves the pattern space when N hits end of input; POSIX sed prints and quits.
Practice what you learned
1. What does the uppercase 'N' command do to the pattern space?
2. How does lowercase 'n' behave when auto-print is enabled?
3. After 'N' joins two lines, how do you match the boundary between them in a substitution?
4. Why is '$!N' often used instead of a bare 'N'?
5. Does 'n' or 'N' restart the sed script from the first command?
Was this page helpful?
You May Also Like
Multiline Processing
Learn how sed handles multiple lines at once using the pattern space, the hold space, and the D, P, N, H, and G commands to perform edits that span line boundaries.
Branching and Loops in sed
Learn how sed uses labels, unconditional branches, and conditional branches to build loops and control flow inside its scripting language.
Backreferences and Groups
Master capturing parts of a match with groups and reusing them via backreferences in both the pattern and the replacement of sed substitutions.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics