Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 38 ยท One-liners

sed One-liners: A Practical Collection

A tested collection of sed one-liners with explanations: spacing, numbering, deleting and printing lines, trimming, joining, reversing, and selective editing.

A one-liner is a complete solution that fits on one command line. They are the reason people love sed: delete trailing spaces, number lines, print the 10th line, join continuation lines, all in a handful of characters. The risk is memorising incantations without understanding them, so every entry below comes with a reading of the command. Once you can read them, you can invent your own.

All examples are GNU sed (the one on Linux and in our sandbox). Where BSD/macOS sed differs, the text says so. Each runnable block creates its own input, so you can edit and re-run.

Spacing

Bash Blank lines

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.

How to read sed G: the hold space is a second buffer, empty at the start. G appends a newline plus the hold space to the pattern space, so each printed line is followed by an empty line. The x;p;x trick swaps pattern and hold space to print an empty line, then swaps back.

Numbering

Bash Line numbers

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.

Printing and deleting by position

Bash Selecting lines

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.

3q prints three lines and quits (it stops reading, so it is fast on huge files). 4~4 is a GNU step address: start at line 4, then every 4th. $!d means "on every line that is not the last, delete".

Selecting by pattern

Bash Pattern-based printing

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 empty regex // in //!p means "the last regex used", which is a short way to skip the start and end lines of the range.

Trimming and cleaning

Bash Whitespace

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.

cat -A shows tabs as ^I and line ends as $ so you can see the effect. The :a / ta loop keeps substituting while the line is shorter than the width; & is the matched text.

Substitution tricks

Bash Selective substitution

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.

Joining, reversing, rearranging

Bash Multi-line edits

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 famous sed ':a;N;$!ba;s/\n/,/g' reads: label a; append the next line (N); if this is not the last line ($!) branch back to a; at the end replace every newline with a comma. It builds the whole file in the pattern space first, so avoid it on very large files.

Safety notes

  • Test with -n and p before using d or -i.
  • sed -i rewrites the file and cannot be undone: use -i.bak until you trust the command.
  • \U, \u, 0,/re/, first~step and \b are GNU extensions; they will not work in macOS sed (install GNU sed as gsed).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does sed G file do?

    G appends a newline and the hold space to the pattern space. The hold space starts empty, so each line is followed by a blank line.
  2. 2. What does sed '$!d' file print?

    $! means 'not the last line', so every line except the last is deleted.
  3. 3. Which command prints lines 4 to 6 only?

    -n turns off automatic printing and p prints just the addressed range.
  4. 4. Why use sed 3q file instead of sed -n 1,3p file on a huge file?

    q exits immediately, which saves time on large inputs.

Summary

  • Read one-liners piece by piece: address, command, then flags.
  • Spacing and numbering use G, = and N; selecting lines uses addresses and -n ... p.
  • $! negates "last line"; N, D, P and :a/ta loops handle multi-line work.
  • Trim whitespace with s/^[ \t]*// and s/[ \t]*$//.
  • GNU extensions are convenient but not portable; test before using -i.

Next: AWK One-liners