You are on page 1of 14

sed, a stream editor

version 3.02, 28 June 1998

by Ken Pizzini

Inc.Suite 330. MA 02111-1307. Published by the Free Software Foundation. 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. 59 Temple Place . Copyright c 1998 Free Software Foundation. Boston. Permission is granted to copy and distribute modi.

provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. under the above conditions for modi. Permission is granted to copy and distribute translations of this manual into another language.ed versions of this manual under the conditions for verbatim copying.

except that this permission notice may be stated in a translation approved by the Foundation. .ed versions.

Chapter 3: sed Programs 1 1 Introduction sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a .

sed works by making only one pass over the input(s). and is consequently more eÆcient. But it is sed's ability to .le or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed).

`-n' `--quiet' `--silent' By default. sed will print out the pattern space at then end of each cycle through the script. `-e script' `--expression=script' Add the commands in script to the set of commands to be run while processing the input. then exit. These options disable this automatic printing. `-h' `--help' Print a usage message brie y summarizing these command-line options and the bug-reporting address. `-f script-. 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. and sed will only produce output when explicitly told to via the p command.lter text in a pipeline which particularly distinguishes it from other types of editors.

le ' `--file=script-.

le ' Add the commands contained in the .

le script-.

then the . -f.le to the set of commands to be run while processing the input. If no -e. or --file options are given on the command-line. --expression.

rst 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 .

A .les to be processed.

The standard input will processed if no .le name of .refers to the standard input stream.

le names are speci.

ed. .

--expression. passed in by one or more of the -e. or the .Chapter 3: 3 sed Programs 2 sed Programs A sed program consists of one or more sed commands. -f. and --file options.

rst non-option argument if zero of these options are used. this will be understood to mean the in-order catenation of all of the scripts and script-. This document will refer to \the" sed script.

le s passed in. followed by a one-character command name and any additional command-speci. Each sed command consists of an optional address or address range.

(Note that counts lines continuously across all input . 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.c code.

les.) `.

rst~step' `$' sed This GNU extension matches every stepth line starting with line .

lines will be selected when there exists a non-negative n such that the current line-number equals .rst. In particular.

one would use 1~2. to select the oddnumbered lines.rst + (n * step). to pick every . 2~3 would be used. to pick every third line starting with the second. Thus.

use 10~5. This address matches the last line of the last .fth line starting with the tenth. and 50~0 is just an obscure way of saying 50.

`\%regexp%' (The % may be replaced by any other single character. since it avoids the tedious escaping of every /. each must be escaped by a backslash (\). This is particularly useful if the regexp itself contains a lot of /s.le of input. If regexp itself includes any delimiter characters. but allows one to use a di erent delimiter than /. `/regexp/' This will select any line which matches the regular expression regexp. `/regexp/I' `\%regexp%I' The I modi.) This also matches the regular expression regexp. each must be escaped by a backslash (\). If regexp itself includes any / characters.

then only lines matching that address are matched. If no addresses are given.er to regular-expression matching is a GNU extension which causes the regexp to be matched in a case-insensitive manner. An address range can be speci. then all lines are matched. if one address is given.

).ed by specifying two addresses separated by a comma (. An address range matches lines starting from where the .

then checking for the ending match will start with the line following the line which matched the . and continues until the second address matches (inclusively).rst address matches. If the second address is a regexp.

rst address. If the second address is a number less than (or equal to) the line matching the .

then only the one line is matched.rst address. .

Chapter 3: sed Programs 3 Appending the ! character to the end of an address speci.

for now see any of the various other documentations for regular expressions. but there are commands for moving data between the pattern and hold spaces.2 conformant) may only support a single one-line comment. 3. That is. sed reads in one line from the input stream and places it in the pattern space. In \normal" operation.]] 3.2 Overview of regular expression syntax [[I may add a brief overview of regular expressions at a later date.cation will negate the sense of the match. This pattern space is where text manipulations occur.4 Often used commands If you use sed at all. The hold space is initially empty. you will quite likely want to know these commands. such as the awk info page. If you are concerned about portability. 3. perhaps perversely. for the null address. This also works for singleton addresses. and then only when the very . be aware that some implementations of sed (which are not POSIX. and. if the ! character follows an address range.3 Where sed bu ers data sed maintains two data bu ers: the active pattern space. the comment continues until the next newline. `#' [No addresses allowed. and the auxiliary hold space. then only lines which do not match the address range will be selected.] The # \command" begins a comment.

Warning: if the .rst character of the script is a #.

If you want to put a comment in the .rst two characters of the sed script are #n. then the -n (noautoprint) option is forced.

`s/regexp/replacement/ ags ' (The / characters may be uniformly replaced by any other single character within any given s command.rst line of your script and that comment begins with the letter `n' and you do not want this behavior. inclusive) references.) 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. the replacement can contain unescaped & . or place at least one space before the `n'. The s command attempts to match the pattern space against the supplied regexp. Also newlines may appear in the regexp using the two character sequence \n. Also. then be sure to either use a capital `N'. which refer to the portion of the match which is contained between the nth \( and its matching \). 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.

Chapter 3: sed Programs 4 characters which will reference the whole matched portion of the pattern space. To include a literal \. or newline in the . &.

The s command can be followed with zero or more of the following ags : `g' Apply the replacement to all matches to the regexp. not just the . be sure to precede the desired \.nal replacement. or newline in the replacement with a \. &.

`p' If the substitution was made. then print the new pattern space.rst. `w . `number ' Only replace the number th match of the regexp.

then write out the result to the named .le-name ' If the substitution was made.

or avoid use of the p command (and also the p ag to the s command). regardless. `{ commands }' A group of commands may be enclosed between { and } characters.le. will double-print lines when auto-print is not disabled and the p command is given. `y/source-chars /dest-chars /' (The / characters may be uniformly replaced by any other single character within any given y command. Note that the current pattern space is printed if auto-print is not disabled.] Exit sed without processing any more commands or input. then. 3. some very small yet useful sed scripts can be built with these commands. Portable sed scripts should thus avoid relying on either behavior. replace the pattern space with the next line of input. If there is no more input then sed exits without processing any more commands. and so neither way can be considered to be in error. `q' [At most one address allowed.) This is particularly useful when you want a group of commands to be triggered by a single address (or address-range) match. Other implementations will only print the line once. immediately start next cycle. Note: some implementations of sed.5 Less frequently used commands Though perhaps less frequently used than those in the previous section. print the pattern space. either use the -n option and explicitly print what you want. `d' Delete the pattern space.) .2 standard. such as this one. `I' (This is a GNU extension. `n' If auto-print is not disabled.) Match regexp in a case-insensitive manner. Both ways conform with the POSIX. This command is usually only used in conjunction with the -n command-line option. `p' Print out the pattern space (to the standard output). (The } must appear in a zero-address command context.

] Immediately output the lines of text which follow this command (each but the last ending with a \. provide that each instance is escaped by a \. or newlines can appear in the source-chars or dest-chars lists. which will be removed from the output). if no addresses were speci. which will be removed from the output) to be output at the end of the current cycle. `a\' `text' `i\' `text' `c\' `text' [At most one address allowed.Chapter 3: sed Programs 5 Transliterate any characters in the pattern space which match any of the sourcechars with the corresponding character in dest-chars. The source-chars and dest-chars lists must contain the same number of characters (after de-escaping). or when the next input line is read. Delete the lines matching the address or address-range.] Queue the lines of text which follow this command (each but the last ending with a \. 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. Instances of the / (or whatever other character is used in its stead). [At most one address allowed.

since the pattern space will have been deleted. `=' [At most one address allowed. A new cycle is started after this command is done. with a trailing \ character to indicate the split.] Print out the current input line number (with a trailing newline). `r .ed). long lines are split. `l' Print the pattern space in an unambiguous form: non-printable characters (and the \ character) are printed in C-style escaped form. the end of each line is marked with a $.

lename ' `w .

lename ' `D' [At most one address allowed.] Queue the contents of .

or when the next input line is read. Note that if .lename to be read and inserted into the output stream at the end of the current cycle.

lename cannot be read. it is treated as if it were an empty .

without any error indication. Write the pattern space to .le.

lename. The .

lename will be created (or truncated) before the .

all w commands (including instances of w ag on successful s commands) which refer to the same .rst input line is read.

Delete text in the pattern space up to the .lename are output through the same FILE stream.

.rst newline. otherwise start a normal new cycle. If any text is left. restart cycle with the resultant pattern space (without reading a new line of input).

Chapter 6: Other resources for learning about sed 6 `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 .

But check out the scripts in the testsuite directory. In all other respects. and these commands can enable one to write quite convoluted scripts. `H' Append a newline to the contents of the hold space. and the amazing dc. and then append the contents of the hold space to that of the pattern space. use of these commands indicates that you are probably better o programming in something like perl.]] 5 About the (non-)limitations on line length For those who want to write portable sed scripts. `: label ' [No addresses allowed. and then append the contents of the pattern space to that of the hold space.rst newline. sorry. `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.sed script in the top-level directory of this distribution. The label may be omitted.] Specify the location of label for the b and t commands. But occasionally one is committed to sticking with sed. 4 Some sample scripts [[Not this release. `G' Append a newline to the contents of the pattern space. `b label ' Unconditionally branch to label. `x' Exchange the contents of the hold and pattern spaces. 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. `g' Replace the contents of the pattern space with the contents of the hold space. a no-op. The POSIX. `h' Replace the contents of the hold space with the contents of the pattern space. The label may be omitted.6 Commands for die-hard sed programmers In most cases. in which case the next cycle is started.2 standard speci. 3. in which case the next cycle is started.

GNU sed has no built-in limit on line length. .es that conforming sed implementations shall support at least 8192 byte line lengths. 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).

Chapter 7: Reporting bugs 6 Other resources for learning about 7 sed In addition to several books that have been written about sed (either speci.

cally or as chapters in books which discuss shell programming). one can .

dbnet. Be sure to include the word \sed" somewhere in the \Subject:" .uk/sedtut10.wollery. To subscribe.htm http://www.ntua.ptug.txt There is an informal \seders" mailing list manually maintained by Al Aab. send e-mail to af137@torfree.nd out more about sed (including suggestions of a few books) from the FAQ for the seders mailing list.gr/~george/sed/sedfaq.demon.org.org/sed/sedfaq.net with a brief description of your interest.ece.html http://www.co. available from any of: http://www. 7 Reporting bugs Email bug reports to bug-gnu-utils@gnu.

.eld.

. . . . . . . . . . . Branch to a label. . . . . . . . . . . . . . . . . . . . . Append pattern space to hold space . . . . . . reporting . . numeric . . . . Bugs. . . . . . . last line . . . . . . . . . . . . . . . . . Address. . . . Append next input line to pattern space . . . . . . . . . . . . . . . . . . . . . . . . . . . in regular expressions . . if s/// succeeded . . . . . . . . . C Case-insensitive matching . . . . . . . . . . . . . . . . . . . . . . . . . Addresses. . . . B Backreferences. . . . . . . Bu er spaces. Address. Append hold space to pattern space . . . . . . . . . . . . . . . unconditionally . . . . . . . . . . . . . . . . A Adding a block of text after a line . . with the exception of the sed commands and command-line options. . . . . . . . pattern and hold . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Branch to a label. Address. . . . . . . . . . . . . . . . . . . . . . . . .Concept Index 8 Concept Index This is a general index of all issues discussed in this manual. . . . . . . . . . Caveat | #n on . . . . . . . . . . . . . . . in sed scripts . . . . . . . as a regular expression . . . Addtional reading about sed . . . . . . . . . . . . . .

. . . . . . . . . Copy pattern space into hold space . . . . Conditional branch . . . . .rst line . . Caveat | p command and -n ag . . . . Comments. . . . . . . . . . . . . . . Command groups . . . . . . . . . . . . . . . . . . . . . . . in scripts . . . . . . . 5 2 2 2 2 7 6 6 6 3 6 6 3 7 4 3 4 4 3 6 6 6 D Delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Copy hold space into pattern space . . . .

. . . . .rst line from pattern space . . . . . . . . 5 Deleting lines . . . . . . . . . . . . 4 E Exchange hold space with pattern space . . . . . . . . . . . . . 4 GNU extensions. . . . . . . . . . . . . . . . . . . . . . 6 Excluding lines . . . . . . . . 6 G Global substitution . . . . . . . . . . . . . . . . . . . . . . . I modi. . . . . . . . . . . 2 F Files to be processed as input . . . . . . . . . . . . . 1 Flow of control in scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

copy into pattern space . . . . . n~m addresses . . 2. . . . . . . appending to pattern space . . . . . . . . . . . . . 4 GNU extensions. . . . . . 2 GNU extensions. . . . . . . . . . . copying pattern space into . . . . . . Hold space. de. . . . . . . . . . . . . . . . . . . . . Hold space.er . Hold space. 6 Grouping commands . . . . . 6 Goto. appending from pattern space . . . . . . . . . . . . . . . . . . . . Hold space. . . . . . . Hold space. . . . . unlimited line length . . . . . . . . . . . . . . . . 4 H Hold space. . . in scripts . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . 6 6 6 6 3 6 I Insert text from a . . . . exchange with pattern space . . . . . . .nition . . . . . . . . . .

append to pattern space . . . . . . . . 6 2 5 2 2 2 2 5 N Next input line. . . . . selecting by regular expression match . . . Line. . . . . . . . . . . . . . . . . . . . . . .le . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Line selection . . . List pattern space . . . . . . Line number. . . . . . . . selecting last . . . . . . . . . . . . . . . . . . Line. . . . . . . . . . . . 5 Inserting a block of text before a line . . . . selecting . . . . . . . . . . . . . . . . . . . . . . . . . . . . Line. . . . . . . . 6 Next input line. . . . . . . . . . 5 L Labels. . . . . . . . . . . . . . . . . . . . selecting by number . . . . . . . . . . . . . . . . . . . . . . . . . . . replace pattern space with . Last line. . . . 4 . . . . . . . . . . print . . . . . . . . . . . . . . . . . . . . . . . in scripts . . . . . . . . . . . . . . . .

. . . . .Concept Index 9 P Parenthesized substrings . . . . . . . . . de. . . . . . . . . . 3 Pattern space. .

. . . . . . . . . . . . . 3 Portability. . . . . . . . . . . . . . . . . . . . . .nition . . . . . . . . . . . . . . . . . . . comments . . . p command and -n ag . . . . . 6 Portability. . . . . . . . . . . . . 4 Print . 3 Portability. . . . . line length limitations . . .

... ... . from a . . . . ... . .... . . . . . . . . 6 Print line number . . . . . . ... .. .. .. . .. . . . .. . . . .. . . . .. . . . .. . . . .. . .. .. . 5 Printing text after substitution . . .. . . . . ... . . .. . 4 Print unambiguous representation of pattern space . . ... .. . . . .... .. .. . . ... .... . . 5 Print selected lines ..rst line from pattern space . . . . .. 4 Q Script. . . . . . . . .. .

. . selecting . . . . . . . . . . . . . . . . . . . . . . . . pattern and hold . . . . . . . . . . . Slash character. . . . . . . . . . . . . . . . . . . . . . . . . Spaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 2 5 2 2 2 2 3 1 1 3 4 Quitting . . . . . Several lines. . sed program structure . . . . replacing . . . . . . . . . in regular expressions . Substitution of text . . . . . . . . . . . 5 Transliteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Standard input. . Read next input line . . . . . . . . . . . . . . . . . processing as input . . . . . . . . . . . . . . . . . . . Substitution of text. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Stream editor . . 4 Range of lines . . . . . . . . . Selecting lines to process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . from command line . . . . Script. . . . . . . . . . . . . . . 4 T R Text. . . . . . . . . . . . . . . . . . . . . Read text from a . . . . . . . . insertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Text. . . . Selected lines. options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Selecting non-matching lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . appending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .le . . . . . . . . . . . . . . . . . .

. . . . . . . . Replace speci. . Replace pattern space with copy of hold space . . . Replace hold space with copy of pattern space . . . . . .le . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . 2 U Usage summary. . . Replacing text matching regexp . . . . . . . . . . . Replacing text matching regexp. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Replacing all text matching regexp in a line . . . . . . 1 V Version. . . . . options . . . . . . . . . . . . . . printing . . . . . . . . . . . . . . printing . . . . Replacing only nth match of regexp in a line . . . . . . . . S 2 4 5 6 6 5 4 4 3 4 Script structure . . . . . . . . . . . . . . 1 W Write result of a substitution to . .c input lines . . . . . . . . . . . . . . . . .

. 4 Write to a . . . . . .le . . . . . . .

. . . . . . . . . . . . . 5 . . . . . . . . . .le . . . . . . . . . . . .

. . . . . . = = (print line number) command . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . 4 T t (conditional branch) command . . . . . .. . . . . . . . . . .. . . 6 p (print) command . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . .. . . . . . . . . . 3 --expression . . . . . .. .. .. --version . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . 6 . . . . . . . . . 5 Q q (quit) command . . . . . . . . . . .. . . .. . . . . . . . . ... .. . . . . .. . . . . . . . . . . . . . . . . . . -h . . . . . . . . . . . . .. .. . . . . . . .. . . . . . . . . . . . . . . --quiet . . . . . .. . . . . . . . . . . . . . .. . . . . . .. . . . . . . . . . . . . 6 I i (insert text lines) command {} command grouping . . . . -n. . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 3 1 6 5 G (appending Get) command . . option flags . . . . . .. . . . . . . . . .. . . . . . . . . . . . . . . . . . . .. . . . .. . . . . .. .. . . . . . . . . . -f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Command and Option Index 10 Command and Option Index This is an alphabetical list of all sed commands and command-line opions. . . . . forcing from within a script . . .. . . . . . . . . . .. . 4 r (read file) command . . . . . . . . . . . 5 N N (append Next line) command . . . . . .. . . . .. . . .. . . . . . 5 d (delete) command . . . . .. . . . . .. . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . .. 4 P . . . . . . . . . . . .. . .. . . .. . .. . . . . . . . 5 D . . . 5 s (substitute) command . . .. . . . . . . . . . . .. . . 6 n (next-line) command . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . .. .. . . . . 6 h (hold) command .. .. . . . . . . . .. . . . . . . . . . . . . . . . .. . . 4 A 5 L l (list unambiguously) command . . -V . . . . .. . . . . . . # # (comment) command . . . . . . .. . . . . --help . . . . . .. . 6 c (change to text lines) command . . . . . . . .. .. . . . . . . . . . . . . . .. . .. . . . . . . . . . .. .. . . . . . . . . . . .. . . . . . . . . . . . 4 B R C S b (branch) command . . . . . .. . . . . . . . . . .. . . . . .. . . . . . . . . . . . --file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 { a (append text lines) command G D (delete first line) command . . . . . . . . . . . . . . . . . . 6 g (get) command . . . . . . . . . . .. . . . . . . . . . . . . . . 6 H H (append Hold) command . . . . . . . . . . . . . . . . . . . . . . 3 s command. . . . . . . . . . . . . . . -e . . . . . .. . . . . . . . . . . . . . . : : (label) command . . . . . . . . --silent . . . . . . . . .. . P (print first line) command . . . . . . . . . . . . . . .. . . . . . . .. . . . . . -n . . . . . . . . . . .. . . . . .. . .. . . . . . . . . . . . .. . . . . . . .. . . . . .

. . . . . . . . . . . 5 X 11 x (eXchange) command . . . . . . . . . . . . . . . . . .Command and Option Index W w (write file) command . . . . . . . . . . . . . . . . . . . . . . . . . . 4 . . . . 6 Y y (transliterate) command . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 3. . . . . . . . . . . . . . . . . . . . . . . . .2 3. . . . . . . . . . . . . . . . . . . . . . . . . . Commands for die-hard sed programmers . . . . . Often used commands . 7 Concept Index . . 6 6 Other resources for learning about sed . . . . . . .i Table of Contents 1 Introduction . . . . . . . . . . . . . .1 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Command and Option Index . . . 2 3. . . . . . . . . Overview of regular expression syntax . 2 3 3 3 4 6 4 Some sample scripts . . . . . . . . . . . . .5 3. Where sed bu ers data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 Selecting lines with sed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 5 About the (non-)limitations on line length . . . . . . . . . . . . . . . . . . Less frequently used commands . . . . . . . 1 2 Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 3 sed Programs . . . . . . . . . . . . . 7 7 Reporting bugs . . . . . 10 . . . . . . . . . . . . . . . . . .3 3. . . . . . . . . . . . . .