sed, a stream editor

version 3.02, 28 June 1998

by Ken Pizzini

Copyright \begin{matrix}c \\ ◯ \end{matrix} 1998 Free Software Foundation, Inc.

Published by the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions, except that this permission notice may be stated in a translation approved by the Foundation.

1 Introduction

sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

2 Invocation

sed may be invoked with the following command-line options:

‘-V’
‘--version’

Print out the version of sed that is being run and a copyright notice, then exit.

‘-h’

‘--help’ Print a usage message briefly summarizing these command-line options and the bug-reporting address, then exit.

‘-n’
‘--quiet’
‘--silent’

By default, sed will print out the pattern space at then end of each cycle through the script. These options disable this automatic printing, and sed will only produce output when explicitly told to via the p command.

‘-e script’

‘--expression=script’

Add the commands in script to the set of commands to be run while processing the input.

‘-f script-file’ ‘--file=script-file’

Add the commands contained in the file script-file to the set of commands to be run while processing the input.

If no -e, -f, --expression, or --file options are given on the command-line, then the first non-option argument on the command line is taken to be the script to be executed.

If any command-line parameters remain after processing the above, these parameters are interpreted as the names of input files to be processed. A file name of - refers to the standard input stream. The standard input will processed if no file names are specified.

3 sed Programs

A sed program consists of one or more sed commands, passed in by one or more of the -e, -f, --expression, and --file options, or the first non-option argument if zero of these options are used. This document will refer to “the” sed script; this will be understood to mean the in-order catenation of all of the scripts and script-files passed in.

Each sed command consists of an optional address or address range, followed by a one-character command name and any additional command-specific code.

3.1 Selecting lines with sed

Addresses in a sed script can be in any of the following forms:

‘number’ Specifying a line number will match only that line in the input. (Note that sed counts lines continuously across all input files.)

‘first~step’ This GNU extension matches every stepth line starting with line first. In particular, lines will be selected when there exists a non-negative n such that the current line-number equals first + (n * step). Thus, to select the oddnumbered lines, one would use 1~2; to pick every third line starting with the second, 2~3 would be used; to pick every fifth line starting with the tenth, use 10~5; and 50~0 is just an obscure way of saying 50.

‘$’ This address matches the last line of the last file of input.

‘/regexp/’ This will select any line which matches the regular expression regexp. If regexp itself includes any / characters, each must be escaped by a backslash (\).

‘\%regexp%’ (The % may be replaced by any other single character.) This also matches the regular expression regexp, but allows one to use a different delimiter than /. This is particularly useful if the regexp itself contains a lot of /s, since it avoids the tedious escaping of every /. If regexp itself includes any delimiter characters, each must be escaped by a backslash (\).

‘/regexp/I’ ‘\%regexp%I’

The I modifier to regular-expression matching is a GNU extension which causes the regexp to be matched in a case-insensitive manner.

If no addresses are given, then all lines are matched; if one address is given, then only lines matching that address are matched.

An address range can be specified by specifying two addresses separated by a comma (,). An address range matches lines starting from where the first address matches, and continues until the second address matches (inclusively). If the second address is a regexp, then checking for the ending match will start with the line following the line which matched the first address. If the second address is a number less than (or equal to) the line matching the first address, then only the one line is matched.

Appending the ! character to the end of an address specification will negate the sense of the match. That is, if the ! character follows an address range, then only lines which do not match the address range will be selected. This also works for singleton addresses, and, perhaps perversely, for the null address.

3.2 Overview of regular expression syntax

[[I may add a brief overview of regular expressions at a later date; for now see any of the various other documentations for regular expressions, such as the awk info page.]]

3.3 Where sed buffers data

sed maintains two data buffers: the active pattern space, and the auxiliary hold space. In “normal” operation, sed reads in one line from the input stream and places it in the pattern space. This pattern space is where text manipulations occur. The hold space is initially empty, but there are commands for moving data between the pattern and hold spaces.

3.4 Often used commands

If you use sed at all, you will quite likely want to know these commands.

‘#’ [No addresses allowed.] The # “command” begins a comment; the comment continues until the next newline. If you are concerned about portability, be aware that some implementations of sed (which are not POSIX.2 conformant) may only support a single one-line comment, and then only when the very first character of the script is a #. Warning: if the first two characters of the sed script are #n, then the -n (noautoprint) option is forced. If you want to put a comment in the first line of your script and that comment begins with the letter ‘n’ and you do not want this behavior, then be sure to either use a capital ‘N’, or place at least one space before the ‘n’.

‘s/regexp/replacement/flags’ (The / characters may be uniformly replaced by any other single character within any given s command.) The / character (or whatever other character is used in its stead) can appear in the regexp or replacement only if it is preceded by a \ character. Also newlines may appear in the regexp using the two character sequence \n. The s command attempts to match the pattern space against the supplied regexp. If the match is successful, then that portion of the pattern space which was matched is replaced with replacement. The replacement can contain \n (n being a number from 1 to 9, inclusive) references, which refer to the portion of the match which is contained between the nth \( and its matching \). Also, the replacement can contain unescaped & characters which will reference the whole matched portion of the pattern space. To include a literal \, &, or newline in the final replacement, be sure to precede the desired \, &, or newline in the replacement with a \. The s command can be followed with zero or more of the following flags:

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

‘p’ If the substitution was made, then print the new pattern space.

‘number’ Only replace the numberth match of the regexp.

‘w file-name’ If the substitution was made, then write out the result to the named file.

‘I’ (This is a GNU extension.)

Match regexp in a case-insensitive manner.

‘q’ [At most one address allowed.] Exit sed without processing any more commands or input. Note that the current pattern space is printed if auto-print is not disabled.

‘d’ Delete the pattern space; immediately start next cycle.

‘p’ Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option. Note: some implementations of sed, such as this one, will double-print lines when auto-print is not disabled and the p command is given. Other implementations will only print the line once. Both ways conform with the POSIX.2 standard, and so neither way can be considered to be in error. Portable sed scripts should thus avoid relying on either behavior; either use the -n option and explicitly print what you want, or avoid use of the p command (and also the p flag to the s command).

‘n’ If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input. If there is no more input then sed exits without processing any more commands.

‘{ commands }’ A group of commands may be enclosed between { and } characters. (The } must appear in a zero-address command context.) This is particularly useful when you want a group of commands to be triggered by a single address (or address-range) match.

3.5 Less frequently used commands

Though perhaps less frequently used than those in the previous section, some very small yet useful sed scripts can be built with these commands.

‘y/source-chars/dest-chars/’ (The / characters may be uniformly replaced by any other single character within any given y command.)

Transliterate any characters in the pattern space which match any of the sourcechars with the corresponding character in dest-chars. Instances of the / (or whatever other character is used in its stead), \, or newlines can appear in the source-chars or dest-chars lists, provide that each instance is escaped by a \. The source-chars and dest-chars lists must contain the same number of characters (after de-escaping).

‘a\’

‘text’ [At most one address allowed.] Queue the lines of text which follow this command (each but the last ending with a \, which will be removed from the output) to be output at the end of the current cycle, or when the next input line is read.

‘i\’

‘text’ [At most one address allowed.] Immediately output the lines of text which follow this command (each but the last ending with a \, which will be removed from the output).

‘c\’

‘text’ Delete the lines matching the address or address-range, and output the lines of text which follow this command (each but the last ending with a \, which will be removed from the output) in place of the last line (or in place of each line, if no addresses were specified). A new cycle is started after this command is done, since the pattern space will have been deleted.

‘=’ [At most one address allowed.] Print out the current input line number (with a trailing newline).

‘l’ Print the pattern space in an unambiguous form: non-printable characters (and the \ character) are printed in C-style escaped form; long lines are split, with a trailing \ character to indicate the split; the end of each line is marked with a $.

‘r filename’ [At most one address allowed.] Queue the contents of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read. Note that if filename cannot be read, it is treated as if it were an empty file, without any error indication.

‘w filename’ Write the pattern space to filename. The filename will be created (or truncated) before the first input line is read; all w commands (including instances of w flag on successful s commands) which refer to the same filename are output through the same FILE stream.

‘D’ Delete text in the pattern space up to the first newline. If any text is left, restart cycle with the resultant pattern space (without reading a new line of input), otherwise start a normal new cycle.

‘N’ Add a newline to the pattern space, then append the next line of input to the pattern space. If there is no more input then sed exits without processing any more commands.

‘P’ Print out the portion of the pattern space up to the first newline.

‘h’ Replace the contents of the hold space with the contents of the pattern space.

‘H’ Append a newline to the contents of the hold space, and then append the contents of the pattern space to that of the hold space.

‘g’ Replace the contents of the pattern space with the contents of the hold space.

‘G’ Append a newline to the contents of the pattern space, and then append the contents of the hold space to that of the pattern space.

‘x’ Exchange the contents of the hold and pattern spaces.

3.6 Commands for die-hard sed programmers

In most cases, use of these commands indicates that you are probably better off programming in something like perl. But occasionally one is committed to sticking with sed, and these commands can enable one to write quite convoluted scripts.

‘: label’ [No addresses allowed.] Specify the location of label for the b and t commands. In all other respects, a no-op.

‘b label’ Unconditionally branch to label. The label may be omitted, in which case the next cycle is started.

‘t label’ Branch to label only if there has been a successful substitution since the last input line was read or t branch was taken. The label may be omitted, in which case the next cycle is started.

4 Some sample scripts

[[Not this release, sorry. But check out the scripts in the testsuite directory, and the amazing dc.sed script in the top-level directory of this distribution.]]

5 About the (non-)limitations on line length

For those who want to write portable sed scripts, be aware that some implementations have been known to limit line lengths (for the pattern and hold spaces) to be no more than 4000 bytes. The POSIX.2 standard specifies that conforming sed implementations shall support at least 8192 byte line lengths. GNU sed has no built-in limit on line length; as long as sed can malloc() more (virtual) memory, it will allow lines as long as you care to feed it (or construct within it).

6 Other resources for learning about sed

In addition to several books that have been written about sed (either specifically or as chapters in books which discuss shell programming), one can find out more about sed (including suggestions of a few books) from the FAQ for the seders mailing list, available from any of:

      http://www.dbnet.ece.ntua.gr/~george/sed/sedfaq.html
      http://www.ptug.org/sed/sedfaq.htm
      http://www.wollery.demon.co.uk/sedtut10.txt

There is an informal “seders” mailing list manually maintained by Al Aab. To subscribe, send e-mail to af137@torfree.net with a brief description of your interest.

7 Reporting bugs

Email bug reports to bug-gnu-utils@gnu.org. Be sure to include the word “sed” somewhere in the “Subject:” field.

Concept Index 8

Concept Index

This is a general index of all issues discussed in this manual, with the exception of the sed commands and command-line options.

A

Adding a block of text after a line. . . . . . . . . . . . . . 5 Address, as a regular expression. . . . . . . . . . . . . . . . 2 Address, last line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Address, numeric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Addresses, in sed scripts . . . . . . . . . . . . . . . . . . . . . . 2 Addtional reading about sed . . . . . . . . . . . . . . . . . . 7 Append hold space to pattern space . . . . . . . . . . . . 6 Append next input line to pattern space . . . . . . . . 6 Append pattern space to hold space . . . . . . . . . . . . 6

B

Backreferences, in regular expressions . . . . . . . . . . 3 Branch to a label, if s/// succeeded . . . . . . . . . . . . 6 Branch to a label, unconditionally. . . . . . . . . . . . . . 6 Buffer spaces, pattern and hold . . . . . . . . . . . . . . . . 3 Bugs, reporting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

C

Case-insensitive matching . . . . . . . . . . . . . . . . . . . . . 4 Caveat — #n on first line . . . . . . . . . . . . . . . . . . . . . 3 Caveat — p command and -n flag . . . . . . . . . . . . . . 4 Command groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Comments, in scripts. . . . . . . . . . . . . . . . . . . . . . . . . . 3 Conditional branch . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Copy hold space into pattern space. . . . . . . . . . . . . 6 Copy pattern space into hold space. . . . . . . . . . . . . 6

D

Delete first line from pattern space . . . . . . . . . . . . . 5 Deleting lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

E

Exchange hold space with pattern space . . . . . . . . 6 Excluding lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

F

Files to be processed as input . . . . . . . . . . . . . . . . . . 1 Flow of control in scripts . . . . . . . . . . . . . . . . . . . . . . 6

G

Global substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 GNU extensions, I modifier . . . . . . . . . . . . . . . . . 2, 4 GNU extensions, n~m addresses . . . . . . . . . . . . . . . 2 GNU extensions, unlimited line length . . . . . . . . . 6 Goto, in scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Grouping commands . . . . . . . . . . . . . . . . . . . . . . . . . . 4

H

Hold space, appending from pattern space . . . . . . 6 Hold space, appending to pattern space. . . . . . . . . 6 Hold space, copy into pattern space . . . . . . . . . . . . 6 Hold space, copying pattern space into . . . . . . . . . 6 Hold space, definition . . . . . . . . . . . . . . . . . . . . . . . . . 3 Hold space, exchange with pattern space. . . . . . . . 6

I

Insert text from a file . . . . . . . . . . . . . . . . . . . . . . . . . 5 Inserting a block of text before a line . . . . . . . . . . . 5

L

Labels, in scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Last line, selecting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Line number, print . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Line selection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Line, selecting by number . . . . . . . . . . . . . . . . . . . . . 2 Line, selecting by regular expression match . . . . . 2 Line, selecting last . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 List pattern space . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

N

Next input line, append to pattern space . . . . . . . 6 Next input line, replace pattern space with . . . . . 4

Concept Index 9

P

Parenthesized substrings . . . . . . . . . . . . . . . . . . . . . . 3 Pattern space, definition. . . . . . . . . . . . . . . . . . . . . . . 3 Portability, comments . . . . . . . . . . . . . . . . . . . . . . . . . 3 Portability, line length limitations . . . . . . . . . . . . . . 6 Portability, p command and -n flag . . . . . . . . . . . . . 4 Print first line from pattern space . . . . . . . . . . . . . . 6 Print line number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Print selected lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Print unambiguous representation of pattern space

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Printing text after substitution . . . . . . . . . . . . . . . . 4

Q

Quitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

R

Range of lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Read next input line . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Read text from a file . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Replace hold space with copy of pattern space . . 6 Replace pattern space with copy of hold space . . 6 Replace specific input lines . . . . . . . . . . . . . . . . . . . . 5 Replacing all text matching regexp in a line. . . . . 4 Replacing only nth match of regexp in a line …. 4 Replacing text matching regexp . . . . . . . . . . . . . . . . 3 Replacing text matching regexp, options . . . . . . . . 4

S

Script structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Script, from a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Script, from command line. . . . . . . . . . . . . . . . . . . . . 1 sed program structure . . . . . . . . . . . . . . . . . . . . . . . . 2 Selected lines, replacing . . . . . . . . . . . . . . . . . . . . . . . 5 Selecting lines to process . . . . . . . . . . . . . . . . . . . . . . 2 Selecting non-matching lines . . . . . . . . . . . . . . . . . . . 2 Several lines, selecting. . . . . . . . . . . . . . . . . . . . . . . . . 2 Slash character, in regular expressions . . . . . . . . . . 2 Spaces, pattern and hold . . . . . . . . . . . . . . . . . . . . . . 3 Standard input, processing as input . . . . . . . . . . . . 1 Stream editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Substitution of text . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Substitution of text, options . . . . . . . . . . . . . . . . . . . 4

T

Text, appending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Text, insertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Transliteration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

U

Usage summary, printing . . . . . . . . . . . . . . . . . . . . . . 1

V

Version, printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

W

Write result of a substitution to file . . . . . . . . . . . . 4 Write to a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Command and Option Index 10

Command and Option Index

This is an alphabetical list of all sed commands and command-line opions.

#

# (comment) command . . . . . . . . . . . . . . . . . . . . . . . . . 3

-

--expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 --file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 --help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 --quiet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 --silent. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 --version. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 -e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 -f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 -h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 -n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

-n, forcing from within a script. . . . . . . . . . . . . 3

-V . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

:

: (label) command. . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

=

= (print line number) command . . . . . . . . . . . . . . . 5

{

{} command grouping . . . . . . . . . . . . . . . . . . . . . . . . . 4

A

a (append text lines) command . . . . . . . . . . . . . . . 5

B

b (branch) command. . . . . . . . . . . . . . . . . . . . . . . . . . . 6

C

c (change to text lines) command. . . . . . . . . . . . . 5

D

D (delete first line) command . . . . . . . . . . . . . . . 5

d (delete) command. . . . . . . . . . . . . . . . . . . . . . . . . . . 4

G

                                                G (appending Get) command . . . . . . . . . . . . . . . . . . . 6

g (get) command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

H

H (append Hold) command. . . . . . . . . . . . . . . . . . . . . . 6 h (hold) command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

I

                                                i (insert text lines) command . . . . . . . . . . . . . . . 5

L

                                                l (list unambiguously) command . . . . . . . . . . . . . . 5

N

                                                N (append Next line) command . . . . . . . . . . . . . . . . 6

n (next-line) command . . . . . . . . . . . . . . . . . . . . . . . 4

P

                                                P (print first line) command . . . . . . . . . . . . . . . . 6

p (print) command. . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Q

q (quit) command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

R

r (read file) command. . . . . . . . . . . . . . . . . . . . . . . . 5

S

s (substitute) command . . . . . . . . . . . . . . . . . . . . . . 3 s command, option flags. . . . . . . . . . . . . . . . . . . . . . 4

T

                                                t (conditional branch) command . . . . . . . . . . . . . . 6

Command and Option Index 11

W

w (write file) command. . . . . . . . . . . . . . . . . . . . . . . 5 x (eXchange) command . . . . . . . . . . . . . . . . . . . . . . . . 6

Y

y (transliterate) command . . . . . . . . . . . . . . . . . . . 4

Table of Contents

1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 Invocation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 3 sed Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

3.1 Selecting lines with sed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 3.2 Overview of regular expression syntax. . . . . . . . . . . . . . . . . . . . . 3 3.3 Where sed buffers data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 3.4 Often used commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 3.5 Less frequently used commands. . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3.6 Commands for die-hard sed programmers . . . . . . . . . . . . . . . . . 6

4 Some sample scripts . . . . . . . . . . . . . . . . . . . . . . . 6 5 About the (non-)limitations on line length . . 6 6 Other resources for learning about sed . . . . . . 7 7 Reporting bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Concept Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Command and Option Index. . . . . . . . . . . . . . . . . . 10