We use sed to perform simple manipulation on data as a stream - it's a Stream EDitor. It generally operates on the stream line by line but can be made to operate on a multi-line buffer if needed. Here are a bundle of command line examples with sed.

Common Usage

The -e switch defines a patter to edit with and -i is used for inline-replacement on files - be careful!

Replace Text

The most common thing to do with sed. First replaces all the instances of Alan with Bill, second only when Carl is in the line too, third when Dirk is not present.

sed -e 's/Alan/Bill/g'
sed -e '/Carl/s/Alan/Bill/g'
sed -e '/Dirk/!s/Alan/Bill/g'

Strip Comments, Blank Lines

The first pattern strips comments (starting with # to end of line), then strip trailing white-space, then blank lines. The second example just uses % as the comment character.

sed -e 's/#.*//' -e 's/[ \t]*$//' -e '/^$/ d' /etc/hosts
sed -e 's/%.*//' -e 's/[ \t]*$//' -e '/^$/ d' /etc/jabber/ejabberd.cfg

Trim Whitespace

First replaces leading whitespace, second replaces trailing whitespace, thrid does both at the same time.

sed 's/^[ \t]*//'
sed 's/[ \t]*$//'
sed 's/^[ \t]*//;s/[ \t]*$//'

Convert Line Endings

sed 's/.$//'

Pack Lines

These patterns can be used for things like minifying CSS or JavaScript. Or just making mass updates to code formatting

First join line pairs, then join lines that end with ,\n

sed 'N;s/\n/ /'
sed -i -e :a -e '$!N; s/,\n/,/; ta'

Adding Line Numbers

sed -

See Also