A stream editor.
sed [ -n ] Script [ File ... ]
sed [ -n ] [ -e Script ] ... [ -f ScriptFile ] ... [ File ... ]
The sed command modifies lines from the specified File parameter according to an edit script and writes them to standard output. The sed command includes many features for selecting lines to be modified and making changes only to the selected lines.
The sed command uses two work spaces for holding the line being modified: the pattern space, where the selected line is held; and the hold space, where a line can be stored temporarily.
An edit script consists of individual subcommands, each one on a separate line. The general form of sed subcommands is the following:
[address-range] function[modifiers]
The sed command processes each input File parameter by reading an input line into a pattern space, applying all sed subcommands in sequence whose addresses select that line, and writing the pattern space to standard output. It then clears the pattern space and repeats this process for each line specified in the input File parameter. Some of the sed subcommands use a hold space to save all or part of the pattern space for subsequent retrieval.
When a command includes an address (either a line number or a search pattern), only the addressed line or lines are affected by the command. Otherwise, the command is applied to all lines.
An address is either a decimal line number, a $ (dollar sign), which addresses the last line of input, or a context address. A context address is a regular expression similar to those used in the ed command except for the following differences:
Certain commands called addressed commands allow you to specify one line or a range of lines to which the command should be applied. The following rules apply to addressed commands:
| -e Script | Uses the Script variable as the editing script. If you are using just one -e flag and no -f flag, the -e flag can be omitted. |
| -f ScriptFile | Uses the ScriptFile variable as the source of the edit script. The ScriptFile variable is a prepared set of editing commands applied to the File parameter. |
| -n | Suppresses all information normally written to standard output. |
This command returns the following exit values:
| 0 | Successful completion. |
| >0 | An error occurred. |
sed "s/happy/enchanted/g" chap1This command sequence replaces each occurrence of the word happy found in the file chap1 with the word enchanted. The g character at the end of the s subcommand tells the sed command to make as many substitutions as possible on each line. Without the g character, the sed command replaces only the first occurrence of the word happy on a line.
The sed command operates as a filter. It reads text from standard input or from the files named on the command line (chap1 in this example), modifies this text, and writes it to standard output. Unlike most editors, it does not replace the original file. This makes the sed command a powerful command when used in pipelines.
pr chap2 | sed "s/Page *[0-9]*$/(&)/" | enqThis command sequence encloses the page numbers in parentheses before printing the file chap2. The pr command puts a heading and page number at the top of each page, then the sed command puts the page numbers in parentheses, and the enq command prints the edited listing.
The sed command pattern /Page *[0-9]*$/ matches page numbers that appear at the end of a line. The s subcommand changes this to (&), where the & stands for the page number that was matched.
sed -n "/food/p" chap3The sed -n displays each line in the file chap3 that contains the word food. Normally, the sed command copies every line to standard output after it is edited. The -n flag stops the sed command from doing this. You then use subcommands like p to write specific parts of the text. Without the -n flag, this example displays all the lines in the file chap3, and it shows each line containing food twice.
:join /\\$/{N s/\\\n// b join }This sed script joins each line that ends with a \ (backslash) to the line that follows it. First, the pattern /\\$/ selects a line that ends with a \ for the group of commands enclosed in {} (braces). The N subcommand then appends the next line, embedding a new-line character. The s/\\\n// deletes the \ and embedded new-line character. Finally, b join branches back to the label :join to check for a \ at the end of the newly joined line. Without the branch, the sed command writes the joined line and reads the next one before checking for a second \.
cat oldfile | sed -e "s/testpattern/$REPL/g" | tee newfile
$ sed -f command.file input.filewhere command.file is the script file and input.file is the input file.
$cat command.file y/ABC\n/abcZ/Alternatively, the following command can also be executed for the same function:
sed "y/ABC\n/abcZ/" input.file