01. Introduction to sed

Sed (short for stream editor) is a non-interactive command line program that takes some text input, performs some command, and prints the results to standard out. Its support for regular expressions makes it extremely powerful, and is often the choice of tool when filtering or transforming text.

To call the sed program, simply use the sed command.

Sed is a pretty big deal!

sed is such a versatile and powerful command line tool that there are entire books written on it. Most people use sed for just search and replace operations, but there are quite a few other useful commands.

How does sed work?

The steps in processing a sed invocation can be broken down to the following four parts:

  1. Sed reads a line from the input stream (from a file, pipe or standard in). and stores it into an internal buffer called pattern buffer or pattern space.
  2. The specified sed command is executed on the pattern buffer line.
  3. Modified contents are outputted and displayed to standard out.
  4. The pattern buffer is emptied, and this process is repeated on all lines until the file is exhausted.
Figure of sed operations: read, execute, display.
How sed works: read, execute, display.

There is also another memory area known as the hold buffer or the hold space. Data stored here do not get deleted, and are used for later retrieval. Although sed clears out the pattern buffer after processing every line, it does not remove the hold buffer until all lines are exhausted.

Sample text

For this tutorial, let's use the following text titled oneOS.txt. Save the following text to a file called oneOS.txt to follow through our examples.

One OS to rule them all,
One OS to find them.
One OS to call them all,
And in salvation bind them.
In the bright land of Linux,
Where the hackers play.

Great! Time to try out some sed!

Let's try out sed

To use sed, simply invoke the command, with the first argument being a string of commands. As we'll see, this may include line addresses, a sed command (deletion, substitution, printing, etc.), flags, and more.

Before we try out the sed command, note that it may read from a file, or from read from standard in.

$ sed '' oneOS.txt
$ cat oneOS.txt | sed ''
$ sed '' < oneOS.txt 

We prefer the first method (if possible), as it uses less resources and is easier to manage. The bottom command would fall under form of a Useless Use of Cat

Let's try sed out with no commands at all and see what it does.

$ sed '' oneOS.txt
One OS to rule them all,
One OS to find them.
One OS to call them all,
And in salvation bind them.
In the bright land of Linux,
Where the hackers play.    

We can see here how sed operates. It reads from the input file, stores the first line in the pattern buffer, then applies the sed command on the pattern buffer. In this case, there is no command, so no operation is performed. The (un)modified content is then outputted to standard output, and this loop is repeated for the second, third, fourth lines until the end of file.

Great! You're on your way to become a sed guru. Let's now see how to specify line addresses to print specific lines out.

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Ad