Tille - I can see you, read those man pages!   Tille's Site

Sed for dummies

Sed derives its name from another fabulously original Unix-way concatenation, Stream EDitor. In other words: you feed text (or lines) to sed and it does things with that input, such as taking out lines containing a certain pattern, replacing a pattern (or searchstring, if you want) in a line, adding strings and so on.

If you were just looking for the simple explanations, this is it. But sed can do much more than just these simple examples. Entire books have been written about this editor, but here's the short version.

The sed command uses regular expressions. For more info, see a.o. the manpages for the GNU grep command, as we have on a standard Linux, and the man pages for your shell

Printing lines containing a pattern

This is something you can do with grep, of course, but you can't do find and replace using that command. This is just to get you started.

This is our example text file:

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.

And we want sed to find all the lines containing our search pattern, in this case erors. We use the p command to obtain this:

yourprompt> 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.

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 to sed:

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

Taking out lines containg a pattern

We use the same example text file. Now we want sed to only print the lines not containing a pattern:

yourprompt> sed '/erors/d' example
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 to sed results in excluding lines containing your search string in the output of the sed program.

Ranges of lines

Still the same example file. Other occasions may happen, but for the sake of the example, let's use the cat command to have some line numbers:

yourprompt> 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 erors are making me sick.
5  This is a line not containing any errors.
6  This is the last line.

This time we want to take out the lines containing the errors, in this case line 2 to 4. Use the d command in sed to do this:

yourprompt> 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.

To print the file starting from a certain line until the end of the file, use a command similar to this:

yourprompt> sed '3,$d' example
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.

Find and replace with sed

Still the same example file. We replace the string erors with the correct spelling of this word:

yourprompt> 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.

As you can see, this is not exactly the desired effect: in line 4, only the first occurence of the search string has been replaced, and there is still an 'eror' left. Use the g command to indicate to search and replace that it should examine the entire line instead of stopping at the first occurence of your string and then presuming that that will probably the only one:

yourprompt> 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.

There are some special cases, which you will know about once you've read docs on regular expressions, but here are 2 of the most popular:

To insert a string at the beginning of each line of a file:

yourprompt> 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.

Handy for quoting.

Insert some string at the end of each line:

yourprompt> 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

Multiple find and replace commands are separated with individual -e options:

yourprompt> cat example | sed -e 's/erors/errors/g' -e 's/last/final/g'
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.

Saving output

Keep in mind that sed by default prints its results to the standard output, most likely being your terminal window. If you want to save the output to a file, redirect it:

yourprompt> sed 'some/expression' file > sed_output_in_a_file
Home
© 1995-2010 Machtelt Garrels - tille - Powered by vIm - Best viewed with your eyes - Validated by W3C - Last update 20100511