sed

stream editor (non-interactive) 

Command


SYNOPSIS

sed [-En] script [file ...]

sed [-En] [-e script] ... [-f scriptfile] ... [file ...]


DESCRIPTION

The sed command applies a set of editing commands contained in script to each input file; if more than one file is specified, they are concatenated and treated as a single large file. script is the collective arguments of all -e options and the contents of all scriptfiles. You can specify multiple -e and -f options. Commands are added to script in the order specified. If you did not specify a file, sed reads the standard input.

sed reads each input line into a special area known as the pattern buffer. Certain commands (gGhHx) use a second area called the hold buffer. By default, after each pass through the script, sed writes the final contents of the pattern buffer to the standard output.

Options

-E 

uses extended regular expressions. Normally, sed uses Basic regular expressions. For more information, see regexp.

-e script 

adds the editing commands script to the end of the script.

-f scriptfile 

adds the commands in the file scriptfile (one command per line) to end of the script.

-n 

suppresses default output; output generated by explicit commands (acilpPsr=) is still sent to standard output.

If you need only one script argument, you may omit the -e and use the first form of the command.

Commands

sed commands are similar to those of the interactive text editor ed, except that sed commands view the input text as a stream rather than as a directly addressable file.

Each line of a sed script contains up to two addresses, a single letter command, possible command modifiers and a terminating newline. The newline is optional in script strings typed on the command line.

[addr[,addr]] command [modifiers]

Permissible addressing constructions are:

n 

The number n matches only the nth input line.

$ 

This address matches the last input line.

/regexp/ 

This address selects an input line matching the specified regular expression regexp. You can use any printable character other than the slash (/) to delimit the regular expression by putting a backslash (\) before the first one. For example, to use % to enclose the regular expression, write \%regexp%.

The following command summary shows each sed command with the maximum number of legitimate addresses. A command may be given fewer than the number of addresses specified, but not more.

aa\ 

appends subsequent text lines from the script to the standard output. sed outputs the text before reading the next record. Text lines are terminated by the first line that does not end with a backslash (\). sed does not treat the \ characters on the end of lines as part of the text.

If used on a command line under the MKS KornShell, sed a\ requires that you include a newline character. You must either enclose the entire text in single quotes ('') or use two backslashes together. For example, you could use either

sed -e 'a\^Jtext' file

or

sed -e "a\\^Jtext" file
a,bb [label

branches to label. If you omit label, sed branches to the end of the script.

a,bc\ 
changes

the addressed lines by deleting the contents of the pattern buffer (input line) and sending subsequent text (similar to the a command) to the standard output. When you specify two addresses, sed delays text output until the final line in the range of addresses; otherwise, the behavior would surprise many users. The rest of the script is skipped for each addressed line, except the last.

a,bd 

deletes the contents of the pattern buffer (input line) and restarts the script with the next input line.

a,bD 

deletes the pattern buffer up to and including the first newline, and then restarts the script from the beginning and applies it to the remaining text in the pattern buffer.

a,bg 

grabs a copy of the text in the hold buffer and places it in the pattern buffer, overwriting the original contents.

a,bG 

grabs a copy of the text in the hold buffer and appends it to the end of the pattern buffer after appending a newline.

a,bh 

holds a copy of the text in the pattern buffer by placing it in the hold buffer, overwriting its original contents.

a,bH 

holds a copy of the text in the pattern buffer by appending it to the end of the hold buffer after appending a newline.

ai\ 

inserts text in the output. This command is similar to the a command, except that its text is output immediately.

a,bl 

lists (prints) the pattern buffer (input line) onto the standard output so that non-printable characters are visible. The end-of-line is represented by $, and the characters \\, \a, \b, \f, \r, \t and \v are printed as escape sequences. This command is analogous to the l command in ed. sed folds long lines to suit the output device, indicating the point of folding with a backslash (\).

a,bn 

prints the pattern space on standard output if the default printing of the pattern space is not suppressed (with of the -n option). The next line of input is then read, and the processing of the line continues from the location of the n command in the script.

a,bN 

appends the next line of input to the end of the pattern buffer, using a newline to separate the appended material from the original. The current line number changes.

a,bp 

prints the text in the pattern buffer to the standard output. The -n option does not disable this form of output. If you do not use -n, the pattern buffer is printed twice.

a,bP 

operates like the p command, except that it prints the text in the pattern buffer only up to and including the first newline character.

aq 

quits sed, skipping the rest of the script and reading no more input lines.

ar file 

reads text from file and places it onto the standard output before reading the next input line. The timing of this operation is the same as for the a command. If file does not exist or cannot be read, sed treats it as an empty file.

a,bs/reg/sub/[gpn][w file

substitutes the text string sub for text matching the regular expression reg. Normally, the s command replaces only the first such matching string in each input line. You can use any single printable character other than space or newline instead of the slash (/) to delimit reg and sub. The delimiter itself may appear as a literal character in reg or sub if you precede it with a backslash (\).

If an ampersand (&) appears in sub, sed replaces it with the string matching reg. For more about special characters in regular expressions, see regexp. A \n in reg matches an embedded newline in the pattern buffer (resulting, for example, from an N command).

The command may be followed by a combination of the following:

n 
substitutes only the nth occurrence of regexp.
g 
replaces all non-overlapping occurrences of regexp rather than the default first occurrence.
p 
executes the print (p) command only if a successful substitution occurs.
w file 
writes the contents of the pattern buffer to the end of file, if a substitution occurs.

a,bt [label

branches to the indicated label if any successful substitution has occurred since either reading the last input line or executing the last t command. If you do not specify label, sed branches to the end of the script.

a,bw file 

writes the text in the pattern buffer to the end of file.

a,bx 

exchanges the text in the hold buffer with that in the pattern buffer.

a,by/set1/set2/ 

transliterates any input character occurring in set1 to the corresponding element of set2. The sets must be the same length. You can use any character other than backslash or newline instead of the slash, to delimit the strings.

a,b{ 

groups all commands until the next matching }, so that sed executes the entire group only if the { command is selected by its address(es).

: label 

designates a label that can be the destination of a b or t command.

a,b!cmd 

executes the specified sed cmd only on lines not selected by the addresses.

# 

treats the script line as a comment unless it is the first line in the script. If the first line in a script is #n, it is equivalent to specifying -n on the command line. Any empty script line is also treated as a comment.

a= 

writes the decimal value of the current line number onto the standard output.


EXAMPLES

Here is a filter to switch political allegiance:

sed 's/democrat\(ic\)*/republican/g'

This filter converts backslashes to forward slashes:

sed 's#\\#/#g'

ENVIRONMENT VARIABLES

COLUMNS 

contains the width of the screen in columns. If set, sed uses this value to fold long lines on output; otherwise, sed uses the appropriate value from the terminal database. If that is not valid, sed uses a default of 80.


DIAGNOSTICS

Possible exit status values are:

0 

Successful completion.

1 

Failure because of any of the following:

— missing script
— too many script arguments
— too few arguments
— unknown option
— can't open script file
— label not found in script
— unknown command
— cannot nest ! command
\ must terminate command
— end of file in command
— command needs a label
— badly formed file name
— cannot open file
— insufficient memory to compile command
— bad regular expression delimiter
— no remembered regular expression
— regular expression error
— insufficient memory for buffers
y command must be followed by a printable character as separator
— the strings are the same length
— non-matching { and } commands
— garbage after command
— too many addresses for command
— newline or end-of-file found in pattern
— input line too long
— pattern space overflow during G command
— hold space overflow during H command
— can't chain command

Messages

badly formed file name for "command" command 

The given command required a file name, but its operand did not have the syntax of a file name.

cannot nest "!" command 

A ! command cannot contain a ! command of its own.

"command" command needs a label 

The specified command required a label, but you did not supply one.

must have at least one (non-comment) command 

The input to sed must contain at least one active command (that is, a command that is not a comment).

no remembered regular expression 

You issued a command that tried to use a remembered regular expression, for example, s//abc; however, there is no remembered regular expression yet. To correct this, change the command to use an explicit regular expression.


LIMITS

sed allows a limit of 40 kilobytes per line. It does not allow the NUL character. The maximum length of a global command is 256 characters, including newlines.


PORTABILITY

POSIX.2. x/OPEN Portability Guide 4.0. All UNIX systems. Windows 8.1. Windows Server 2012 R2. Windows 10. Windows Server 2016. Windows Server 2019. Windows 11. Windows Server 2022.

The -E option is an extension to the POSIX and x/OPEN standards.


AVAILABILITY

PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Commands:
awk, diff, ed, grep, gres, vi

Miscellaneous:
regexp


PTC MKS Toolkit 10.4 Documentation Build 39.