6.6 Sed

The ‘sed’ module applies sed-like s-expressions to strings to modify them. It is designed mainly for use in transformation rules (see section Transformations).

6.6.1 Loading sed module

module sed sed [args]

The following arguments may be given in the statement above:


Use case-insensitive expressions.


Use case-sensitive expressions. This is the default.


Use extended regular expressions. This is the default.


Use basic regular expressions.

Although sed module is designed for transformations in the first place, it may also be used as a conventional lookup database (see section Using Sed for Lookups). The following options modify its behavior in this mode:


Use str for positive replies.


Use str for negative replies.


Reply with str if an error occurred.

6.6.2 Defining Sed Databases

The definition of a sed databases requires a single argument: the s-expression to be applied. For example:

database dequote sed 's/<(.*)>/\1/g'

Be sure to properly quote the expression, especially if it contains backreferences. It is preferable to use single quotes, to avoid duplicating each backslash in the expression, as shown in the example below. If the expression itself contains single quote, you may either use double-quotes to quote the entire expression:

database foo sed "s/'utf8'(.*)/u8_\\1/"

or use escaped single quotes outside of quoted expression (a technique familiar for shell programmers):

database foo sed 's/'\''utf8'\''(.*)/u8_\1/'

All options valid for module initialization (see section Loading sed module) may also be used in database declarations. When used so, they take precedence over module initialization options. For example, the following database definition uses basic case-insensitive regular expressions:

database bar sed noextended noicase 's/test(\([^)]\))/\1/g'

6.6.3 S-expressions

The transformation expression is a sed-like replace expression of the form:


where regexp is a regular expression, replace is a replacement for each part of the input that matches regexp. Both regexp and replace are described in detail in The "s" Command: (sed)The "s" Command section `The `s' Command' in GNU sed.

As in sed, you can give several replace expressions, separated by a semicolon.

Supported flags are:


Apply the replacement to all matches to the regexp, not just the first.


Use case-insensitive matching


regexp is an extended regular expression (see Extended regular expressions: (sed)Extended regexps section `Extended regular expressions' in GNU sed).


Only replace the numberth match of the regexp.

Note: the POSIX standard does not specify what should happen when you mix the ‘g’ and number modifiers. The sed module follows the GNU sed implementation in this regard, so the interaction is defined to be: ignore matches before the numberth, and then match and replace all matches from the numberth on.

Any delimiter can be used in lieue of ‘/’, the only requirement being that it be used consistently throughout the expression. For example, the following two expressions are equivalent:


Changing delimiters is often useful when the regex contains slashes. For instance, it is more convenient to write s,/,-, than s/\//-/.

6.6.4 Using Sed for Lookups

The sed module is designed primarily for argument transformation. Nevertheless, it may also be used to define simple look-up databases. When used in a database clause of a dispatch rule, the module behaves as follows. The s-expression is applied to the key. If the result differs from the input key, the ‘positive-reply’ is returned. It the result is the same as the input key, ‘negative-reply’ is returned. If some error occurred, ‘onerror-reply’ is returned. The reply strings may be supplied as arguments to the database definition or to the module loading statement. The following variables are expanded within these strings:


The map name.


The key value.


Transformed key value. This variable is not defined for ‘onerror-reply’.

Default replies are:

Reply Value
positive-reply OK ${xform}
negative-reply NOTFOUND
onerror-reply NOTFOUND