The GNU sed stream editor

Posted by

 Introduction
What is sed?
A Stream EDitor 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 these d 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.
What distinguishessedfrom other editors, such asvianded, is its ability to filter text that it gets from a
pipeline feed. You do not need to interact with the editor while it is running; that is whysedis sometimes called a batch editor. This feature allows use of editing commands in scripts, greatly easing repetitive editing tasks. When facing replacement of text in a large number of files,sed is a great help.
sed commands

Thesedprogram can perform text pattern substitutions and deletions using regular expressions, like the ones
used with thegrepcommand;
The editing commands are similar to the ones used in thevieditor:
Sed editing commands
Command Result

  1. a               Append text below current line.
  2. c              Change text in the current line with new text.
  3. d                Delete text.
  4. i                Insert text above current line.

 The GNU sed stream editor
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 tosed.  Sed 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.
These info pages contain more information; we only list the most frequently used commands and options
here.
 Interactive editing
 Printing lines containing a pattern
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:
sandy ~> cat -n example
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 errors are making me sick.
5 This is a line not containing any erors.
6 This is the last line.
sandy ~>
We want sed to find all the lines containing our search pattern, in this case “erors”. We use the p to obtain theresult:
sandy ~> sed  ‘/erors/p’ example
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.
sandy ~>

The GNU sed stream editor

As you notice,sedprints 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:

sandy ~> sed -n ‘/erors/p’ example
It is a text with erors.
Lots of erors.
So much erors, all these erors are making me sick.
sandy ~>

 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:

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:

sandy ~> sed -n ‘/^This.*errors.$/p’ example
This is a line not containing any errors.
sandy ~

Note that the last dot needs to be escaped in order to actually match. In our example the expression just
matches any character, including the last dot.

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:

sandy ~> sed ‘2,4d’ example
This is the first line of an example text.
This is a line not containing any errors.
This is the last line.
sandy ~>

To print the file starting from a certain line until the end of the file, use a command similar to this:
sandy ~> sed ‘3,$d’ example
This is the first line of an example text.
It is a text with erors.
sandy ~>

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”:

The GNU sed stream editor
sandy ~> sed -n ‘/a text/,/This/p’ example
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.
sandy ~>

Find and replace with sed
In the example file, we will now search and replace the errors instead of only (de)selecting the lines
containing the search string.
sandy ~> sed ‘s/erors/errors/’ example
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.
sandy ~>
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 thegcommand to indicate tosedthat it should examine the
entire line instead of stopping at the first occurrence of your string:
sandy ~> sed ‘s/erors/errors/g’ example
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.
sandy ~>
To insert a string at the beginning of each line of a file, for instance for quoting:
sandy ~> sed ‘s/^/> /’ example
> 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.
sandy ~>
Insert some string at the end of each line:
sandy ~> sed ‘s/$/EOL/’ example
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
sandy ~>

The GNU sed stream editor
Multiple find and replace commands are separated with individual -eoptions:
sandy ~> sed -e ‘s/erors/errors/g’ -e ‘s/last/final/g’ example
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.
sandy ~>
Keep in mind that by defaultsedprints its results to the standard output, most likely your terminal window. If
you want to save the output to a file, redirect it:

sed option ‘some/expression’ file_to_process> sed_output_in_a_file

Leave a Reply

Your email address will not be published. Required fields are marked *