sed is a useful editor that can alter text that matches a particular pattern in a file or I/O stream. The basic syntax is sed script file, where the script contains a pattern to match and replace using ``regular expression'' rules (man regexp). The usage is best illustrated with examples. For more information, check the man pages or that awk and sed book by O'Reilly.
sed s/foo/bar/g fileThe g instructs sed to replace all occurences on a given line, rather than the first as is the default. Note that this invocation directs the output to the screen; use redirection to put it in a new file. Also, any character (except
\
) can be
used in place of / to delimit the target and replacement, so
long as the same character is used throughout. This can be useful
when dealing with pathname substitutions.
sed /doh/s/foo/bar/g file
sed /^doh/s/foo/bar/4 file
sed '/doh$/s/foo/bar/4' fileNote the
''
to hide the special meaning of $ to the
shell.