Manipulating text with sed

Context addresses

Context addresses are regular expressions enclosed in slashes ``/''. If you specify a context address for a command, sed only applies the editing function to those lines which match the regular expression. By using context addresses and a print function, you can improvise a grep-like behavior; for example, the shell script mygrep:

sed -n -e "/$1/p" <$2

This script uses the shell parameter $1 as a context address for sed to use in searching the file specified by the parameter $2. Whenever sed finds a line matching the address given in $1, it executes the p function (print) and outputs that line.

Note the -n argument to sed in this script; sed normally echoes every line it reads to its standard output. While the -n option is in effect, sed only prints when you tell it to with the p or P functions. Note also that if you want to use sed within a shell script and pass parameters to it, the sed instructions must be in double quotes, not single quotes (see ``How the shell works'' for an explanation of shell quoting and its meaning).

For example:

   mygrep charlie /etc/passwd
   charlie::8:5:Charles Stross:/usr/charlie:/usr/bin/ksh
Context addresses are enclosed in slashes (/). They include all the regular expressions common to both ed and sed:
For a context address to ``match'' the input, the whole pattern within the address must match some portion of the pattern space. If you want to use one of the special characters literally, that is, to match an occurrence of itself in the input file, precede the character with a backslash (\) in the command.

Each sed command can have 0, 1, or 2 addresses.

Here are some examples based on the following configuration file (a piece of an /etc/passwd file):
   remacc:x:::Remote access::
   daemon:No login:1:1:Spooler:/usr/spool:
   sys:No login:2:2:System information::
   bin:x:3:3:System administrator:/usr/src:
   xmail:x:4:4:Secret Mail:/usr/spool/pubkey:
   msgs:No login:7:7:System messages:/usr/msgs:
   charlie:x:8:5:Charles Stross:/usr/charlie:/bin/ksh

matches lines 1, 3, 6 in our sample file

matches lines 1, 3, 4, 6, 7

matches line 8

matches no lines

matches all lines

matches no lines
You can use a single address to control the application of a group of commands by grouping the commands with curly braces ({ }). For example:

/red/ {

This short sed script searches for lines containing the regular expression ``red'' and then carries out the grouped commands, which replace the first occurrence of ``red'' with ``green'' and the first instance of ``blue'' with ``yellow'' on each matching line. You might use this script by placing it in a file called and invoking it from a shell as follows:

$ sed -f <input_file >output_file

For more on substitution in sed, including how to apply a change to all instances of a matching string, see ``Substitute functions''.

Next topic: Functions
Previous topic: Line addresses

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003