|
Quick links About
sed Syntax Examples
Related commands Linux / Unix main page
About sed
Sort for Stream Editor sed allows you to use
pre-recorded
commands to make changes to text.
Syntax
sed [ -n ] [ -e script ] ... [ -f script_file ] ... [ file ... ]
| -n |
Suppress the default output. |
| -e script |
script is an edit command for sed . See USAGE below for more information on the format of script. If there is just one -e option and no -f options, the flag -e may be omitted. |
| -f script_file |
Take the script from script_file. script_file
consists of editing commands, one per line. |
| commands |
The sed commands to perform. |
| file |
A path name of a file whose contents will be read and edited. If multiple file operands are
specified, the named files will be read in the order specified and the concatenation will be edited. If no file operands are specified, the standard input
will be used |
Script:
A script consists of editing commands, one per line, of the following form:
[ address [ , address ] ] command [ arguments ]
Zero or more blank characters are accepted before the first address and before command. Any number of semicolons are accepted before the first address.
In normal operation, sed cyclically copies a line of input (less its terminating NEWLINE character) into a pattern space (unless there is something left after a D command), applies in sequence all commands whose addresses select that
pattern space, and copies the resulting pattern space to the standard output (except under -n) and deletes the pattern space. Whenever the pattern space is written to standard
output or a named file, sed will immediately follow it with a NEWLINE character.
Some of the commands use a hold space to save all or part of the pattern space for subsequent retrieval. The pattern and hold spaces will each be able to hold at least 8192 bytes.
sed Addresses:
An address is either empty, a decimal number that counts input lines cumulatively across files, a $ that addresses the last line of input, or a context address, which consists
of a /regular expression/.
A command line with no addresses selects every pattern space.
A command line with one address selects each pattern space
that matches the address.
A command line with two addresses selects the inclusive range from the first pattern space that matches the first address through the next pattern space that matches the second address. Thereafter the process is repeated, looking again for the first address. (If the second address is a number less than or equal to the line number selected by the
first address, only the line corresponding to the first address is selected.)
Typically, address are separated from each other by a comma (,). They may also be separated by a semicolon (;).
Examples sed G myfile.txt > newfile.txt
In the above example using the sed command with G would
double space the file
myfile.txt and output the results to the newfile.txt.
sed = myfile.txt | sed 'N;s/\n/\. /'
The above example will use the sed command to
output each of the lines in myfile.txt with the line number
followed by a period and a space before each line. As done with
the first example the output could be redirected to another file
using > and the file name.
sed 's/test/example/g' myfile.txt > newfile.txt
Opens the file myfile.txt and searches for the
word "test" and replaces every occurrence with the word
"example".
sed -n '$=' myfile.txt
Above this command count the number of lines in
the myfile.txt and output the results.
Related commands
awk
ed
grep
|