Hello! SED - Stream Editor

Stream EDitor (SED) is an important text-processing utilities on GNU/Linux. It uses a simple programming language and is capable of solving complex text processing tasks with few lines of code. This easy, yet powerful utility makes GNU/Linux more interesting.

SED can be used in many different ways, such as:

  • Text substitution,
  • Selective printing of text files,
  • In-a-place editing of text files,
  • Non-interactive editing of text files, and many more.

This tutorial will give you just enough knowledge to read and understand this book, to be a master on the SED, GREP and Find command, you need to explore relevant literature referenced at end of this book.

Sed works as follows: it reads from the standard input, one line at a time. for each line, it executes a series of editing commands, then the line is written to STDOUT.

An example which shows how it works : we use the s sommand. s means “substitute” or search and replace. The format is

sed s/regular-expression/replacement text/{flags} 

In the example below, we have used g as a flag, which means “replace all matches” (global replacement):

$ cat datafile.txt
  I have a big data!
$ sed -e 's/big/small/g' -e 's/data/list/g' datafile.txt
  I have a small list!

Let’s try to learn what happened.

Step 1, sed read in the line of the file and executed


which produced the following text:

    I have a small data!

Step 2, then the second replacement command ('s/data/list/g') was performed on the edited line and the result was:

    I have a small list!

Take it easy!

Get hands-on with 1200+ tech skills courses.