Stream Editing

sed

Batch editor may be used as a filter in one of 2 ways:

sed [ -n ] -f script-file [ file-list ]

sed [ -n ] script [ file-list ]

sed scripts

A script contains one or more lines with the format:

[addr[, addr]] instruction [arg-list]

Lines may be separated with ;

The sed utility processes input as follows:

Whenever the pattern space is written to standard output or a named file, sed will immediately follow it with a NEWLINE character.

Editing Commands

Different commands require different sets of arguments.

a\ text append lines (until finding one not ending in '\') after outputting the pattern space
b label branch to command : label
c\ text change selected line(s) to following text, as in a
d delete line, then read next input line
i\ text insert lines (as in a) before outputting the pattern space
l list, showing all non-printing characters
p print line
q quit
r file read file, copy contents to output
s/old/new/f substitute new for old; if f=g, replace all occurrences; if f=p, print; if f=w file, write to file

Editing Commands (continued)

t label test: branch to label if substitution made to current line
w file write line to file
y/str1/str2/ replace each character from str1 with corresponding character from str2
= print current input line number
!cmd do the sed command only if the current line is not selected
: label set label for b and t commands
{ treat commands up to matching } as a group

Addresses

Lines may be addressed explicitly or using regular expressions

Regular Expressions

Represent patterns to be replaced (for s and y commands) or identifying target input lines (for addresses)