Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

sed - stream editor

&pagelevel(4)&pagelevel

sed is a non-interactive stream editor that provides a similar set of functions to the interactive line editor ed.

sed is a versatile tool that allows you to:

  • perform multiple global editing functions efficiently in one pass through the input

  • easily edit piped command output

  • apply a sequence of editing commands that is too complicated for interactive input.

sed reads files sequentially, edits each line with sed commands you have specified in a sed script, and writes the results on the standard output. The sed script is either read straight from the command line or taken from a file. sed acts as a filter, i.e. it does not change the original input file. If you want to save the changes, you will have to redirect the standard output to a file.


Syntax


Format 1: sed[ -n] script[ file...]
Format 2: sed[ -n][ -e script]...[ -f skriptfile]...[ file...]




Format 1: sed[ -n] script[ file...]


-n

Suppresses the default output, which is to pass every input line to the standard output after processing (see Example 6).

-n not specified:
sed copies each processed input line to the standard output, even if the sed script does not contain an output command such as p. The commands in the sed script determine whether a line is modified by editing instructions. Input lines that are processed by an output command such as p are thus shown twice in succession. First, as part of the default output of all processed lines, and then as the output of the special editing command which causes them to be displayed again.

script

Uses the command-line script to edit the input file. If the script contains blanks,
newlines, or shell metacharacters, it must be enclosed in single quotes: ’script’.

file

Name of the file whose contents are to be processed by sed. The file must be a text file.

file not specified:
sed reads from standard input.



Format 2: sed[ -n][ -e script]...[ -f skriptfile]...[ file...]


-n

Suppresses the default output, which is to pass every input line to the standard output after processing (see Example 6).

-n not specified:
sed copies each processed input line to the standard output, even if the sed script does not contain an output command such as p. The commands in the sed script determine whether a line is modified by editing instructions. Input lines that are processed by an output command such as p are thus shown twice in succession. First, as part of the default output of all processed lines, and then as the output of the special editing command which causes them to be displayed again.

-e script

Uses the command-line script to edit the input file. If the script contains blanks, newlines, or shell metacharacters, it must be enclosed in single quotes: ’script’.

The -e script option may be included a number of times with different scripts and may also be combined with the -f scriptfile option. In this case sed applies the commands from all scripts on each input line.

If the command line contains the -e option only once and does not include option -f, you may omit the -e when you specify script.

-f scriptfile

Reads editing instructions for the input file from the file named scriptfile.

You can include the -f scriptfile option a number of times with different script files and also combine it with -e script. sed then reads the sed commands from all specified sed scripts.

file

Name of the file whose contents are to be processed by sed. The file must be a text file.

file not specified:
sed reads from standard input.

Functionality

sed works on copies of the input lines, successively copying each line into a temporary work area called a "pattern space".

Each line of input is normally processed in the following cycle:

Step 1:

The next (or first) line of input is copied into the pattern space.

Step 2:

All sed script commands that select the pattern space (i.e. address the last line copied into it) are successively applied to its contents. Depending on the editing instruction in the script, the contents of the pattern space are then altered as required, or left unchanged.

Step 3:

The contents of the edited pattern space are then sent to standard output, and the pattern space is cleared.

This cycle is repeated until all input lines have been processed.

The pattern space may occasionally contain multiple lines (see commands g, G, N). However, it is always the address of the last line copied into the pattern space that serves as the pattern space address.

Some sed commands (g, G, h, H) copy text from the pattern space to a temporary storage area called the "hold space". The hold space saves all or part of the pattern space for subsequent retrieval.

Format of a sed script

A sed script consists of command lines in the form:

[range]sed_command[flags]...

or

[range]{sed_command[flags]...
        sed_command[flags]...
        ...
        }


The braces are required only if you specify an address range.
The right brace must be positioned at the start of a line, i.e. only preceded by blanks or tabs.

No blanks are permitted between the range and the sed_command.

A range serves to select particular input lines and can be specified as one or two comma-separated addresses. When the given range "selects" the pattern space, i.e. addresses the last line copied to it, the associated sed command or command list is applied to the pattern space.

For range you can specify one address or two addresses separated by a comma.

range = address

The pattern space that matches address is selected.

range = address1,address2

An inclusive range from the first pattern space that matches address1 through the next pattern space that matches address2 is selected. However, if address2 is a line number in the input file that comes before the one selected with address1, only one line is selected (address1).

range not specified:
The pattern space is always selected, i.e. the last input line copied to it is always addressed.

Addresses

$

last line of the last input file

n

nth input line, where n is a positive integer. Input lines are numbered consecutively across all files.

/pattern/

Input line containing a string matching the specified pattern. Any slash / appearing in pattern must be preceded by a backslash \ to escape it. pattern is a simple regular expression (see Tables and Directories, Regular POSIX shell expressions) with the following modifications:

The construction \?regular expression?, where ? is any character, is identical to /regular expression/. Note that in the address \xabc\xdefx, for example, the second x is escaped and stands for itself, so that the regular expression is abcxdef.

The escape sequence \n matches a newline embedded in a pattern space.

A period matches any character except the terminal newline of the pattern space.

sed commands

The following section contains a list of sed commands, described in alphabetical order.The square brackets [ ] are not to be entered; they merely indicate that the enclosed address or address range is optional.

The text argument consists of one or more lines. All but the final line must end with a backslash (\) to escape the terminating newline.

[address]a\
text

Append text to the pattern space that is output.

[range]b[label]

Branch to the sed command :label in the sed script.

label not specified:
Branch to the end of the sed script.

[range]c\
text

Change. Delete the selected pattern space, send text to the output and start the next cycle.

[range]d

Delete the contents of the pattern space and start the next cycle. Step 3 is dropped, i.e. the contents of the pattern space are not sent to standard output.

[range]D

Delete the initial segment of the pattern space up to (and including) the first newline and start the next cycle.

[range]g

Replace the contents of the pattern space by the contents of the hold space.

[range]G

Append the contents of the hold space to the pattern space.

[range]h

Replace the contents of the hold space by the contents of the pattern space.

[range]H

Append the contents of the pattern space to the hold space.

[address]i\
text

Insert text into the standard output before the contents of the pattern space.

[range]l

List the pattern space on the standard output, representing non-printing characters with replacement characters (e.g. tab characters as the greater-than sign >) or with their two-digit octal ASCII code equivalents in the form \nn (see section “ASCII character set (ISO 646)”). Long lines with more than 71 characters are split into two or more lines (folded). A backslash at the end of a screen line indicates that the text line continues in the next output line.

[range]n

Next. Copy the contents of the pattern space to the standard output and replace them with the next line of input. The address of the last line of input becomes the address of the pattern space.

[range]N

Append the next line of input to the pattern space with an embedded newline. The address of the last appended line becomes the address of the pattern space.

[range]p

Print the contents of the pattern space on the standard output. Non-printing characters are not represented.

[range]P

Print the initial segment of the pattern space, up to the first newline, on the standard output. Non-printing characters are not represented.

[address]q

Quit sed. If you have specified multiple sed scripts, sed quits at the first q encountered in any of the scripts.

[range]r rfile

Read the contents of rfile and send them to the standard output before copying the next input line to the pattern space. rfile must be separated from the sed command r by exactly one space and must come at the end of the command line.

[range]s/RE/repstring/[flags]

Substitute repstring for strings that match the regular expression RE in the pattern space. RE can be specified in the form of a simple regular expression (see section “Regular POSIX shell expressions”). The delimiter does not have to be /: most other characters are accepted. For a fuller description see the s command in ed.

flags

n

where n is an integer between 1 and 512. Substitute repstring for just the nth instance of RE in a line.

g

Globally substitute repstring for all instances of RE in a line.

p

Print the contents of the pattern space on the standard output if a replacement was made. This applies even if sed was invoked with the -n option.

w wfile

Write. Write the pattern space to the file wfile whenever a replacement is made. Any file named wfile that was already present before you called sed will be overwritten. If a number of w commands in a sed script write to the same wfile, the contents of the pattern space will be appended to the existing contents of wfile in each case. wfile must be separated from the sed command w by exactly one space and must come at the end of the command line. A maximum of 10 different files can be used for wfile in any one invocation of sed.


Caution!
If you use the name of your input file as wfile, you will destroy your input file!


No flags specified:
Only the first instance of RE in the line is replaced by repstring.

[range]t label

Test. Branch to the sed command :label in the sed script if any substitutions have been made since the last input line was copied to the pattern space or since the most recent execution of a t command.

label not specified: Branch to the end of the sed script.

[range]w wfile

Write. Write the pattern space to the file wfile. Any file named wfile that was already present before you called sed will be overwritten. If a number of w commands in a sed script write to the same wfile, the contents of the pattern space will be appended to the existing contents of wfile in each case. wfile must be separated from the sed command w by exactly one space and must come at the end of the command line. A maximum of 10 different files can be used for wfile in any one invocation of sed.


Caution!
If you use the name of your input file as wfile, you will destroy your input file!


[range]x

Exchange the contents of the pattern and hold spaces.

[range]y/string1/string2/

Transform. Replace each occurrence of a character in string1 with the corresponding character in string2. string1 and string2 must be of the same length and must be specified explicitly. Regular expressions cannot be used.

[range]!command
[range]!{commandlist}

Don’t command. Apply command to all lines not selected by the specified address range. command may be any sed command or a sed command list enclosed in braces {...}.

:label

Set a label for b and t commands to branch to. label is any string up to 8 characters long.

[address]=

Prints the current line number on the standard output on its own line.

[range]{sed_command
        sed-command
        . . .
       }

Successively execute all sed commands enclosed within the braces {} if the address range selects the current pattern space.

The { can be preceded with blank characters and can be followed with white space. The commands can be preceded by white space. The termination } must be preceded by a newline character and then zero or more blank characters.

<enter>

The newline character is treated as a null command and is ignored. This allows you to produce more transparent sed scripts by using blank lines.

#

If # is the first character entered in the first line of a script file, the entire line is interpreted as a comment.

#n

If the first line of a script file begins with the character sequence #n, the default output of the pattern space is suppressed (as with the -n option). The entire line is treated as a comment, i.e. not interpreted as an sed command.

Error

sed: command garbled: ...

The sed script contains a syntax error. The colon is followed by the script location at which sed terminated.


Can't open file

You have specified a nonexistent input file or a file for which you do not have read permission.


Unrecognized command: ...

The sed script contains an unknown command.

Locale

The following environment variables affect the execution of sed:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_COLLATE

Determine the locale for the behavior of ranges, equivalence classes and multicharacter collating elements within regular expressions.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files) and the behavior of character classes within regular expressions. LC_CTYPE also governs which characters the sed command l treats as non-printing.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Example 1

Write the string XXXXX in all blank lines of a file and redirect the output to another file:

$ sed '/^$/s/^/XXXXX/' file > newfile

/^$/ selects all blank lines, i.e. lines that contain nothing (not even a blank) between start of line and end of line. sed searches for the beginning of the line (^) and replaces it with the string XXXXX.

Example 2

Indent by 4 spaces all lines that begin with a digit, and redirect the output to another file:

$ sed '/^[0-9]/s/^/    /' file > newfile

/^[0-9]/ selects all lines that begin with a digit. sed locates the start of the line (^) and replaces it with 4 blanks ('    '), i.e. shifts the rest of the line 4 positions to the right, padding positions 1 to 4 with spaces.

Example 3

Print all the non-blank lines in a file:

$ sed '/^$/d' file

All blank lines are selected by /^$/ and deleted by d.

Example 4

Make a file double-spaced by adding a blank line after each line in the file:

$ sed 's/$/\ <enter>
> /' file

Since no address is specified, sed searches for the end of the line ($) in every line and replaces it with a newline character, thus adding a blank line after every line.

Example 5

Print the second and third column of a file in which individual columns are separated from one another by a colon. The third column is to be placed before the second:

$ sed 's/[^:]*:\([^:]*\):\([^:]*\):.*/\2:\1/' file

Explanation:

s/ / /

The string between the first and second slash is searched for in each line and replaced
by the string between the second and third slash.

[^:]*

Any number (*) of characters other than a colon ([^:])

:

Colon

\( \)

Brackets off part of expression for reuse in the replacement string which is between the second and third slash.

\2

The replacement string is to begin with the part of the expression which is in the second bracket \(...\).

:

There is to be a colon between the first and second part of the replacement string.

\1

The second part of the replacement string is to be the part of the expression which is in the first bracket \(...\).

Example 6

Filter all professions out of the file personnel and write them into the file named professions under a new heading. The following example works on the assumption that the used search key (Profession:) does not appear in the first line of the file.

The personnel file has the following format:

Name: John Miller
Marital status: Divorced
Profession : Journalist
Name: Catherine Baker
Marital status: Married
Profession: Programmer
etc.

sed program:

$ sed -n '1{s/^.*/Professions:/
            h
          }
          /^Profession:/{s/^Profession: *\(.*\)/\1/
            H
          }
          ${g
            p
          }' personnel > professions

$ cat professions
Professions:
Journalist
Programmer
etc.
$

Explanation:

Line 1 in the pattern space is replaced by the string Professions: and then stored in the hold space by the h command.

Each line in the pattern space that begins with Profession: is replaced by the string that follows it and then appended to the hold space by the H command.

The last line of the file ($) is replaced in the pattern space by the entire contents of the hold space. Finally the pattern space is printed on the standard output by the p command.

The -n option changes the default output so that the input lines copied to the pattern space are not automatically sent to standard output after editing. Only the p command actually prints the pattern space.

See also

awk, ed, grep
Tables and Directories, Regular POSIX shell expressions