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/{ ...do 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.