Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 38 ยท sed Basics

sed Print, Delete and Quit: p, d, q and -n

Use sed to print or delete lines by number and by pattern: the p, d, q and = commands, why -n matters, and how sed's automatic print really works.

Substitution changes text inside lines. The next group of commands works on whole lines: print them, delete them, stop reading, or number them. With these you can build tools like head, tail and grep out of sed, which is a good way to understand how sed really works.

How sed runs your commands

For every input line sed does this cycle:

flowchart TD
    A["read next line into the pattern space"] --> B["run your commands in order"]
    B --> C{"-n given?"}
    C -->|"no"| D["print the pattern space"]
    C -->|"yes"| E["print nothing"]
    D --> F{"more input?"}
    E --> F
    F -->|"yes"| A
    F -->|"no"| G["end"]

The automatic print at the end of the cycle is why p shows a line twice.

Remember this cycle: it explains almost everything about p, d, n and -n.

The p command and -n

p prints the pattern space now. Because sed also prints automatically at the end of the cycle, the line is printed twice unless you suppress the auto-print with -n.

Bash p with and without -n

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The pair -n plus p is how you tell sed: "print only what I say". It is the tool behind sed -n '5,10p' file, a way to show lines 5 to 10.

The d command

d deletes the pattern space and starts the next cycle immediately, so nothing is printed for that line and the commands after d do not run.

Bash Delete lines by number and by pattern

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

There are always two ways to say the same thing: delete what you do not want (/re/d) or print only what you want (-n '/re/p'). Choose whichever reads better. For "all but" tasks, deleting is easier; for "just this" tasks, printing is.

Line numbers and the last line

A bare number is an address that selects one line. $ selects the last line. Counting continues across all input files together (unless you use -s).

Bash First line, last line, specific line

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

; separates commands. 1d;$d means "if the line is 1, delete it; if the line is the last, delete it". This is a handy way to strip a header and a footer.

The q command: quit early

q stops sed after the current line (printing it first, unless -n). It makes sed behave like head, and it is fast on large files because sed stops reading:

Bash head with sed q

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

5{p;q} is a block: when the line is 5, run p then q. Without the q, sed would continue reading the other 95 lines for nothing. The last example shows that q accepts an exit status, handy in scripts. GNU sed also has Q, which quits without printing the current line.

The = command: line numbers

= prints the current line number on its own line. Combined with -n and a pattern, it tells you where a match is, and $= prints the total number of lines (like wc -l).

Bash Where is it? Counting with =

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The last example is a classic trick: the first sed prints the number and the line on separate lines, and the second joins pairs with N (you will learn N in multi-line editing).

Combining with substitution

You can combine line commands with s. Within one script, commands run top to bottom for every line, and a d stops the rest:

Bash A tiny cleanup script

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Three -e options give three commands: remove comment lines, remove blank lines (including lines with only spaces), and tighten = into =. The result is a clean key=value file.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does sed p file print every line twice?

    sed auto-prints the pattern space after running the commands. p adds a second print. Use -n to turn auto-print off.
  2. 2. Which command prints only line 7 and stops reading as soon as possible?

    -n suppresses auto-print, p prints line 7 and q quits so the rest of the file is never read.
  3. 3. What does sed '1d;$d' do?

    1 and $ are separate addresses; each d deletes its line. Together they strip the first and last line.
  4. 4. What does sed -n '$=' print?

    $ selects the last line and = prints its line number, which equals the total number of lines.

Summary

  • sed runs a cycle per line: read, run commands, auto-print (unless -n).
  • p prints, d deletes and starts the next cycle, q quits (optionally with an exit status), = prints the line number.
  • A number selects a line, $ the last line; /re/ selects by pattern.
  • sed -n '/re/p' keeps matches; sed '/re/d' removes them.

Next: Addresses and Ranges