SED

Section: User Commands (1)
Updated: 1989 May 26
Index Return to Main Contents
 

NAME

sed - stream editor  

SYNOPSIS

sed [ -n ] [ -e script ] [ -f scriptfile ] [ files ]  

DESCRIPTION

Sed copies the named files or the standard input to its standard output, while performing certain operations specified in the command line script or in the file scriptfile. If no filenames are specified, the default input is the standard input.

Sed cyclically reads in input lines and places them in the pattern space. The pattern space is selected by some or all of the commands in the script, and acted upon by these commands. The pattern space is then written to the output, and the next input is read.

The script (on the command line or in the script file) is composed of one or more lines that have the following form:

[ range ] function [ arguments ]

The range refers to either a line address range or a regular expression in the style of ed(1). The range selects which input lines the function should act upon. If the range is omitted, all input lines are selected.

The line address is calculated sequentially from the first file to last, in order. A line address range may be either a single line number or two line numbers separated by a comma. A single number selects that line number to act upon, and two line numbers separated by a comma inclusively select all lines between those two numbers.

Regular expressions in sed differ from ed(1) regular expressions in that the escape sequence `\n' matches a newline embedded in the pattern space. A $ character may also be used to select the last line of input.

The following list of sed functions are preceded by a number indicating the maximum amount of line numbers valid in the range of the function. A (1) would indicate that the function can only take effect after a certain line number and not a range of line numbers, while a (2) would indicate that either one line number or two are valid for the function.

When a function takes a text argument, a newline in the text may be escaped by placing a `\' character before it, allowing multiline texts.

Empty commands are ignored.

A wfile or rfile argument to a function must be at the end of a command line, and there must be exactly one space between the command and the file name argument. There can be at most 10 wfile and rfile arguments. All wfile or rfile arguments are created before any input lines are read.

(1)a\

text

Append the text to the output after the current pattern space.
(2)b label
Branch to the label defined with the `:' command. An empty label implies a branch to the end of the script.
(2)c\

text

Change text. The pattern space is unconditionally deleted and the text is written to the output following the range.
(2)d
Delete the pattern space.
(2)D
Delete the initial segment of the pattern space up to and including the first newline. (the pattern space may contain more than one line of text -- see the `N' command)
(2)g
Overwrite the pattern space with the contents of the hold space.
(2)G
Append the contents of the hold space to the pattern space.
(2)h
Hold the pattern space. The pattern space is copied into the hold space.
(2)H
Append the pattern space to the hold space.
(1)i\

text

Insert. Write text to the output.
(2)n
Flush the pattern space to the output and read the next input line into the pattern space.
(2)N
Append the next line of input to the pattern space, inserting a newline character between them.
(2)p
Print. Write a copy of the pattern space to the output.
(2)P
Write a copy of the pattern space up to and including the first newline to the output.
(1)q
Quit and do not start a new cycle.
(2)r rfile
Read the contents of rfile and write them to the output.
(2)s/regular expression/replacement/flags
Search for the regular expression in the pattern space and replace it with the replacement string. Any character may be used as a delimiter instead of the `/'. The flags argument may be any number of the following:
g
Globally substitute the replacement for the regular expression. Normally, only the first match is found, but g forces matching of all occurances.
p
Print the pattern space if a replacement was made.
w wfile
Write the pattern space to the end of wfile if a replacement was made. This must be the last flag argument if it is used.
(2)t label
Test. If a substitution was made since the last time an input line was read into the pattern space, or since the last `t' command, command branches to the label. An empty label means to branch to the end of the script.
(2)w wfile
Write the pattern space to the file wfile.
(2)x
Exchange the contents of the pattern and hold spaces.
(2)y/string1/string2/
Yank all characters in string1 and replace them with the corresponding characters in string2, which should be of equal length as string1.
(2)! function
Apply the function to those lines not selected by the command.
(0): label
Declare a label for `b' and `t' commands to branch to.
(1)=
Write the current line number to the output.
(2){
Perform multiple commands on the range. The `{' character is followed by one or more commands to perform on the pattern space. A '}' character terminates the list of commands.
 

OPTIONS

-n
suppress the default output
-e
indicates that the next argument is a script. Useful for scripts starting with a - character.
-f
indicates that the following argument is a file containing a script of commands to Sed.

 

SEE ALSO

ed(1), grep(1), awk(1), lex(1)

 

AVAILABILITY

GNU sed is free; anyone may redistribute copies of sed to anyone under the terms stated in the GNU General Public License, a copy of which may be found in each copy of GNU Emacs. See also the comment at the beginning of the source code file sed.c.

Copies of GNU sed may sometimes be received packaged with distributions of Unix systems, but it is never included in the scope of any license covering those systems. Such inclusion violates the terms on which distribution is permitted. In fact, the primary purpose of the General Public License is to prohibit anyone from attaching any other restrictions to redistribution of any of the Free Software Foundation programs.


 

Index

NAME
SYNOPSIS
DESCRIPTION
OPTIONS
SEE ALSO
AVAILABILITY

This document was created by man2html, using the manual pages.
Time: 05:23:46 GMT, January 05, 2023