Functions in a sed command



Last revision August 6, 2004

Table of Contents:
  1. Forms of addresses in sed commands
  2. Functions in a sed command
  3. sed examples

This list covers the basic sed functions. See the sed documentation for a complete list.

There are additional commands to operate on more than one line at a time; conditionally execute a function; or temporarily store the contents of a line so it can be moved to a different location in the file. Read the documentation for examples of these.

s Substitute one string for another within the line(s) that matches the address(es). This is probably the most useful single function. Its syntax is just like that of the substitute command in vi (but without the leading colon), that is:
      s/old/new/
where old is a regular expression to match, and new is the text to be substituted in place of old. The slash character is customarily shown as the "delimiter" character marking the ends of the old and new strings, but any character can be used as the delimiter, as long as that character itself is not part of either the old or new strings. For example, the "plus" (+) character is often used as the delimiter when either the old or new string contains a slash character.

By default, only the first occurrence per line of the old string is replaced. If the text or regular expression old may occur more than once in a single line, and you want all those occurrences in each matching line to be replaced by new, then add the optional g argument (stands for "global") at the end of the command, for example,
      s/old/new/g

a Add lines of text to the output after the line that matches the address. This function will only accept a single address, not an address range. The "arguments" to the a function are the lines of text to add. You must specify them as in the following example:
     35a\
     first line of text to add\
     more lines of text ...\
     last line of text to add

The a function name is preceded by an address (line number or regular expression), and followed immediately by a backslash and RETURN key (newline). Then each line of text to be added, except for the last, ends with a backslash (escape character) just before the RETURN.

i Insert lines of text to the output before the line that matches the address. Works like the a function.
c Change the line(s) that matches the address (or address range) to the new lines of text that follows. Syntax like the a function, but allows a range of addresses, not just a single address.
d Delete the line(s) that matches the address(es). Do not include it in the output stream.
p Select the line(s) that matches the address(es) for the output stream. This can be used with the -n option on the sed command line in order to write out only those lines that match a p function.
w Write a copy of the line(s) that matches the address(es) to another file whose name is specified as argument. This does not affect whether the line also goes to the normal output stream. If the specified output file already exists when sed starts up, the previous file contents are destroyed!
r Read the contents of the file whose name is given as argument, and put them on the output stream ahead of the input line(s) that matches the address(es).