Pattern Addressing Examples

Command

Action performed

s/xx/yy/g

Substitute on all lines (all occurrences).

/BSD/d

Delete lines containing BSD.

/^BEGIN/,/^END/p

Print between BEGIN and END, inclusive.

/SAVE/!d

Delete any line that doesn’t contain SAVE.

/BEGIN/,/END/!s/xx/yy/g

Substitute on all lines, except between BEGIN and END.

Braces ({ }) are used in sed to nest one address inside another or to apply multiple commands at a single matched address:

[/pattern/[,/pattern/]]{
command1
command2
}

The opening curly brace must end its line, and the closing curly brace must be on a line by itself. Be sure there are no spaces after the braces.

Get Linux in a Nutshell, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.