sed non-interactive stream editor
Last revision August 6, 2004
Table of Contents: |
sed is used to edit large files or do a complicated sequence of editing commands that can be saved in a "script" (file). It is especially good for making "global" changes; for instance, substituting for all occurrences of a string in a file. It is not interactive. You do not "see" what it is doing.
sed reads the input file line by line, copying the lines to the output file after performing editing changes that apply to that line. It is also possible to select only certain lines for the output, to insert lines or the contents of other files at specified points in the file, or to write a subset of the lines to a second file.
Syntax to invoke sed:
sed [-n]
[-f scriptfile]
[[-e command] ...]
[inputfile(s)]
Optional items are shown in square brackets -- do not type the
brackets themselves. Italicized words are items that you replace with actual values
specific to your application. Ellipses (...) mean that the previous item may be
repeated.
At least one sed editing command must be specified, either on the command line after a -e option flag, or in a script file whose name is specified after the -f option flag.
sed reads all the lines from all the input files listed on the command line, or from standard input if no files are specified, and writes processed or selected lines to standard output. Generally, you want to redirect standard output to a file.
Besides the -e and -f option flags, sed accepts the option flag -n, which tells it not to automatically write each processed input line to the output, but instead to only write lines that are specifically selected by sed editing commands.
You can supply editing and selection commands for sed on the command line, each following its own -e option flag, and generally enclosed in quotes (to protect against shell interpretation of special characters). But unless you have very simple commands, it is generally easier to put them into a "script" file. This script file is then referenced by the -f option on the command line.
Generally, a script file has a single command per line. If a command in this file is too long to fit on one line, you can continue it by using a backslash as the last character of the line to be continued. This backslash character "escapes" the newline character that normally separates lines in a file, so that two lines are treated logically as one. The backslash character itself is not read by the program.
The syntax for an individual editing command in a sed script
(or on the command line) is:
[address1[,address2]]function
arguments
where the brackets indicate optional parts (the brackets themselves are not typed).
Also, the portions of the command (address, function,
and arguments) are concatenated together without any intervening
blanks.