100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

The n and N Commands

Understand how sed's lowercase n and uppercase N commands pull additional input lines into processing, and how they differ in what they do to the pattern space.

Advanced sedIntermediate9 min readJul 10, 2026
Analogies

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

bash
# 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.txt

After '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

Was this page helpful?

Topics covered

#Programming#SedStreamEditorStudyNotes#TheNAndNCommands#Commands#Reading#Ahead#Input#StudyNotes#SkillVeris#ExamPrep