paulgorman.org/technical

sed

sed(1)

sed is a stream editor for filtering and transforming text.

sed makes one pass over the input, operating on one line at a time. It’s especially good to edit piped/redirected text in a command pipleline.

With the -f option, sed gets its edit commands from the specified file, rather than as a command line argument.

Simple Examples

Delete line 99 by line number (line numbering starts at one not zero):

% sed -i '99d' file.txt

Delete a line matching a pattern (first checking what will happen with print):

% sed -n '/my pattern/p' file.txt
% sed -i '/my pattern/d' file.txt

On BSD, we might have to do:

% sed -i'' -e '/my pattern/d' file.txt

Append a line to the file:

% sed -i '$ a My new line.' file.txt

Insert a line before the first line:

% sed -i '1 i My new line.' file.txt

Don’t print matching lines (with incidental use of a POSIX Extended regular expression):

# ls | sed -En '/(foo|bar)/!p'

Insert a line before ONLY the first match: don’t. Use ex or ed instead:

% ex -c'/search string' -c'i|Ex Inserted line' -c'wq' file.txt
% echo '/Search string\ni\nEd inserted line\n.\nw' | ed file.txt

Printing

sed takes options, edit commands, and an input file, like:

sed -n 'p' foo.txt

By default, sed simply echos its input (‘p’). sed foo.txt acts like cat foo.txt.

The ‘p’ command tells it to print. sed 'p' foo.txt would prints each line twice. The -n flag suppressed the default echo, so sed -n 'p' foo.txt prints each line only once.

sed -n '1p' foo.txt prints the first line of the input.

sed -n '1,5p' foo.txt prints lines 1 through 5.

sed -n '1,+4p' foo.txt same as above.

sed -n '1~3p' foo.txt prints the first line, then every third line that follows.

sed -n '5,6p;8p' foo.txt prints lines five, six, and eight.

Deleting

sed '0~2d' foo.txt deletes every other line.

sed -i '0~2d' foo.txt as above, but MODIFIES THE FILE IN PLACE.

sed -i.bak '0~2d' foo.txt as above, but keeps an unmodified .bak file.

Substitution

sed 's/bam/bat/gi' foo.txt substitutes bat in place of bam (case insensitive). (Without the ‘g’ flag, sed only substitutes the first match on a line.)

echo "http://example.com/foo/index.html" | sed 's^com/foo^org/bar^' uses alternate substitution field delineation characters to avoid colliding with the front slashes in the input.

Appending, Inserting, etc.

sed '/barbat/ a\Note the above!' foo.txt appends a line after a line with the matched pattern.

sed '$ a\FIN' foo.txt adds a line at the end of the input.

sed '$ a\Goodbye,\ Goodnight,\ and Good Luck!' foo.txt adds multiple lines.

sed '1 i\Start Here:' foo.txt inserts a line before line 1.

`sed ‘/Old Line/ c\New Line’ changes/replaces every matching line.

sed '=' foo.txt echos the input with added line numbers.

sed -n '/bar/=' foo.txt prints the number of the line that matches the pattern.

sed -n '/bat/!p' foo.txt prints all the lines that do NOT match.

http://www.grymoire.com/Unix/Sed.html