Introduction to SED
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.
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}
Let's Hello World! SED
In the example below, we have used g
as a flag, which means "replace all matches" (global replacement):
-
Input
Let's now try to learn what happened?
Step 1, sed read in the line of the file and executed
s/big/data/g
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!
Note the use of the RegEx (*
that matches any char after the word data
)!