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...] |
Suppresses the default output, which is to pass every input line to the standard output after processing (see Example 6). -n not specified:
Uses the command-line script to edit the input file. If the script contains blanks,
Name of the file whose contents are to be processed by sed. The file must be a text file. file not specified: |
Suppresses the default output, which is to pass every input line to the standard output after processing (see Example 6). -n not specified:
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.
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.
Name of the file whose contents are to be processed by sed. The file must be a text file. file not specified: |
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:
or
The braces are required only if you specify an address range. 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: |
Addresses
last line of the last input file
nth input line, where n is a positive integer. Input lines are numbered consecutively across all files.
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 (
Append text to the pattern space that is output.
Branch to the sed command :label in the sed script. label not specified:
Change. Delete the selected pattern space, send text to the output and start the next cycle.
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.
Delete the initial segment of the pattern space up to (and including) the first newline and start the next cycle.
Replace the contents of the pattern space by the contents of the hold space.
Append the contents of the hold space to the pattern space.
Replace the contents of the hold space by the contents of the pattern space.
Append the contents of the pattern space to the hold space.
Insert text into the standard output before the contents of the pattern space.
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
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.
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.
Print the contents of the pattern space on the standard output. Non-printing characters are not represented.
Print the initial segment of the pattern space, up to the first newline, on the standard output. Non-printing characters are not represented.
Quit sed. If you have specified multiple sed scripts, sed quits at the first q encountered in any of the scripts.
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.
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.
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! No flags specified:
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.
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! [range]x Exchange the contents of the pattern and hold spaces.
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.
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 {...}.
Set a label for b and t commands to branch to. label is any string up to 8 characters long.
Prints the current line number on the standard output on its own line.
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.
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
The sed script contains a syntax error. The colon is followed by the script location at which sed terminated.
You have specified a nonexistent input file or a file for which you do not have read permission.
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:
/^$/ 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:
/^[0-9]/ selects all lines that begin with a digit. sed locates the start of the line (^) and replaces it with 4 blanks ( |
Example 3
Print all the non-blank lines in a 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:
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:
Explanation:
The string between the first and second slash is searched for in each line and replaced
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.
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.
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:
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 |