Using sed: the next level

The utility sed is much more powerful than most people realize. It is underutilized by many.

Consider this: have you ever seen Tetris written in sed? Or Sokoban? Or Arkanoid?

While these games are not comparable to the originals or those that take full advantage of the computer, they prove one point: sed is very powerful.

Another example: the LEAF distributions which run on a floppy or CDROM all use sed to do a lot of the work in the distribution, emulating other much more powerful commands such as grep and who.

Here are some tips for using sed:

  • Don’t use grep, awk, and sed together. Either awk or sed can replace grep utterly; no need to spawn another process. Grep can be replaced with sed by using /pat/p inside of a sed command with the -n (don’t print by default) option.
  • Use the d command to sed to “delete” lines from the output. This will allow you to strip out lines that are not wanted.
  • Use multiple sed commands – by separating each with a semicolon (;) character. One sed process, multiple commands – don’t use multiple sed processes.
  • Want to do some substitutions and other things to only particular lines? Use a construct like this: /patt/{ stuff.. }
  • Learn about the hold space and how to use it. This allows you to take matched patterns from one part of the file and put them elsewhere later.
  • Want to work on more than one line at once? Use the N command: this will append the next line into the current pattern space (with embedded newline). Thus, matching a line with EOF at the end and a blank line on the next would be like this: N; /EOF\n$/d; (this matches the two lines and deletes them…)

One more thing: get the sed and awk book: people are so used to wunderkind like Perl and Ruby that they forget what the originals are really capable of.

10 thoughts on “Using sed: the next level”

  1. “They say” don’t use sed when you can use grep, tr, etc.

    But “not many” say don’t use perl when you can use sed.

    Sed is in “almost all” in base distros. But perl isn’t.

    If only screen had multiple copy+paste buffers, like tmux…

    1. Actually, GNU screen does have multiple copy/paste buffers, though they are called registers. There are commands to move things around into one register or another as well.

  2. ddouhitt: Let’s say you have a string of text that you copy from the terminal. Let’s call it a “register”. It could be, e.g., a URL. GNU Screen can only store one of these clips at a time. tmux can store many of them; one can select from a list of stored buffers (e.g. URLs) to paste them into any given terminal.
    If you believe GNU Screen can do this, let’s see an example. It can’t.

    tmux commands copy (default bindings)
    Ctrl-B then Ctrl-Space then Enter
    GNU screen commands to copy
    Ctrl-A then Ctrl-Space then Enter

    Do this repeatedly over the course of your session.

    tmux will store the last 9 clips by default
    Ctrl-B lsb to see the list

    screen stores only 1 clip

    I’s tempted to also affirm that tmux is much more stable than screen, but I won’t. I can’t prove it in a blog comment. You’ll just have to take a break from screen and see for yourself. In tmux, the dungeon never collpases.

    I will never go back to screen. It was the only alternative for many years and we had to tolerate its weaknesses. Not anymore.

    1. GNU screen has many registers – 27 or more – not just 9. I’ve only seen the “Dungeon collapses.” a few times; it’s normally quite stable.

  3. Ctrl-[ is used by both screen and tmux to initiate copy mode
    screen only uses Space to start selection
    tmux uses Ctrl-Space
    those are the defaults. they can easily be changed

  4. GNU screen has “process” and “readreg” commands, but these don’t come close to the ease of tmux equivalents, which feature auto-completion. When you want a list of your “registers” in GNU screen what do you do? There is no command for this. The dungeon has seen its day.

  5. The number of registers in tmux can be adjusted easily. 9 is the default.

    I respect people’s appreciation of screen. It was a great for so many years and the only one of its kind, up until tmux. But we have a better, more stable program now. What we don’t have is enough people using it so that this fact becomes known.

    Usage, in eaqual ime with screen, will show the user the benefits of tmux. My words are not enough. I have used both for equal time. But I know longtime UNIX users have used screen for much longer.

    This reminds me of BIND users who refuse to take a good look at djbdns. They will never know what they are missing. The only way to evaluate the merits of these programs is to use them in equal time.

    These programs achieve their improvements by incresing more simplicity, not introducing complexity.

  6. tmux is now is the base system of OpenBSD.

    that’s a start. people using OpenBSD will hopefully get some exposure to it.

    tmux is my wm. like sed, the tty is grossly underestimated.

  7. I’m intrigued by tmux. However, it suffers from one serious drawback (mentioned in the FAQ as well): it lacks support for UNIX systems like HP-UX.

    Another drawback (though I don’t know why): it’s not packaged for Debian/Ubuntu – which tends to contain every open software program known to mankind. Seems odd to me, but there it is.

    Those who want more information can go to the tmux home page at

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: