The sed utility reads the specified files, or the standard input if no files are specified, modifying the input as specified by a list of commands. The input is then written to the standard output.
sed, a stream editor
====================
[Stream EDitor](http://www.gnu.org/software/sed/manual/sed.html) is used to
perform basic transformations on text read from a file or a pipe. The result is
sent to standard output. The syntax for the sed command has no output file
specification, but results can be saved to a file using output redirection. The
editor does not modify the original input.
---
[TOC]
---
The editing commands are similar to the ones used in the vi editor:
**Editing Commands**
| Command | Result |
| ------- | ---------------------------------------------- |
| `a\` | Append text below current line. |
| `c\` | Change text in the current line with new text. |
| `d` | Delete text. |
| `i\` | Insert text above current line. |
| `p` | Print text. |
| `r` | Read a file. |
| `s` | Search and replace text. |
| `w` | Write to a file. |
Apart from editing commands, you can give options to `sed`. An overview is in
the table below:
**Options**
| Option | Effect |
| ------ | --------------------------------------------------------------------------------------------------------------- |
| `-e` | SCRIPT Add the commands in SCRIPT to the set of commands to be run while processing the input. |
| `-f` | Add the commands contained in the file SCRIPT-FILE to the set of commands to be run while processing the input. |
| `-n` | Silent mode. |
| `-V` | Print version information and exit. |
This is something you can do with `grep`, of course, but you can't do a **find
and replace** using that command. This is just to get you started.
This is our example text file, `example.txt`.
$ cat -n example.txt
1 This is the first line of an example text.
2 It is a text with erors.
3 Lots of erors.
4 So much erors, all these erors are making me sick.
5 This is a line not containing any errors.
6 This is the last line.
### Printing lines containing a pattern
Find all the lines containing search pattern, in this case `erors`. Use the `p`
to obtain the result:
$ sed '/erors/p' example.txt
This is the first line of an example text.
It is a text with erors.
It is a text with erors.
Lots of erors.
Lots of erors.
So much erors, all these erors are making me sick.
So much erors, all these erors are making me sick.
This is a line not containing any errors.
This is the last line.
As you notice, sed prints the entire file, but the lines containing the search
string are printed twice. This is not what we want. In order to only print those
lines matching our pattern, use the `-n` option:
$ sed -n '/erors/p' example.txt
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.
### Deleting lines of input containing a pattern
We use the same example text file. Now we only want to see the lines not
containing the search string:
$ sed '/erors/d' example.txt
This is the first line of an example text.
This is a line not containing any errors.
This is the last line.
The `d` command results in excluding lines from being displayed.
Matching lines starting with a given pattern and ending in a second pattern are
showed like this:
$ sed -n '/^This.*errors.$/p' example.txt
This is a line not containing any errors.
### Ranges of lines
This time we want to take out the lines containing the errors. In the example
these are lines `2` to `4`. Specify this range to address, together with the `d`
command:
$ sed '2,4d' example.txt
This is the first line of an example text.
This is a line not containing any errors.
This is the last line.
To print the file starting from a certain line until the end of the file, use a
command similar to this:
$ sed '3,$d' example.txt
This is the first line of an example text.
It is a text with erors.
This only prints the first two lines of the example file.
The following command prints the first line containing the pattern `a text`, up
to and including the next line containing the pattern `a line`:
$ sed -n '/a text/,/This/p' example.txt
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.
This is a line not containing any errors.
### Find and replace
In the example file, we will now search and replace the errors instead of only
*deselecting* the lines containing the search string.
$ sed 's/erors/errors/' example.txt
This is the first line of an example text.
It is a text with errors.
Lots of errors.
So much errors, all these erors are making me sick.
This is a line not containing any errors.
This is the last line.
As you can see, this is not exactly the desired effect: in line `4`, only the
first occurrence of the search string has been replaced, and there is still an
`eror` left. Use the `g` command to indicate to `sed` that it should examine the
entire line instead of stopping at the first occurrence of your string:
$ sed 's/erors/errors/g' example.txt
This is the first line of an example text.
It is a text with errors.
Lots of errors.
So much errors, all these errors are making me sick.
This is a line not containing any errors.
This is the last line.
To insert a string at the beginning of each line of a file, for instance for
quoting:
$ sed 's/^/> /' example.txt
This is the first line of an example text.
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.
This is a line not containing any errors.
This is the last line.
Insert some string at the end of each line:
$ sed 's/$/EOL/' example.txt
This is the first line of an example text.EOL
It is a text with erors.EOL
Lots of erors.EOL
So much erors, all these erors are making me sick.EOL
This is a line not containing any errors.EOL
This is the last line.EOL
Multiple find and replace commands are separated with individual `-e` options:
$ sed -e 's/erors/errors/g' -e 's/last/final/g' example.txt
This is the first line of an example text.
It is a text with errors.
Lots of errors.
So much errors, all these errors are making me sick.
This is a line not containing any errors.
This is the final line.
---
### More examples
**Replace all occurrences of** `day` **with** `night`
$ sed 's/day/night/g' file.txt
**Replace all occurrences of** `day` **with** `night` **within** `file.txt`
$ sed -i 's/day/night/g' file.txt
**Replace all occurrences of** `day` **with** `night` **on stdin**
$ echo 'It is daytime' | sed 's/day/night/g'
**Remove leading spaces**
$ sed -i -r 's/^\s+//g' file.txt
**Remove empty lines and print results to stdout.**
$ sed '/^$/d' file.txt
---
**Sources**
- [Bash-Beginners-Guide](http://linux.die.net/Bash-Beginners-Guide/sect_05_01.html)