The Sed command is a very powerful tool that is used for viewing, editing, and modifying files.
It can be used for a variety of purposes like adding or removing the lines from a file, editing the lines in a file, searching for a pattern in a file, and much more.
In this tutorial we’ll learn the basics of the stream editor (sed
) command through practical examples.
The general structure of searching and replacing with sed
is:
sed 's/<text to search>/<replacement text>/' <inputfile>
You can achieve more with options like -i
,b
, d
, and ^
, which we’ll cover in this article.
Let’s dive into the tutorial to see how to apply the sed command.
Table of Contents
How the Sed Command Works
The sed command lets you to replace specific or all instances of a text. You can also combine the command with other commands such as the cat, find and awk. The sed command modifies a copy of the file unless you supply it with the i
flag.
Let’s see how to achieve the missions in various ways.
Replace all instances of text
Replace a copy
Let’s create a file, file.txt, using the cat
command.
cat > file.txt
and add the following sentences.
I love Linux. I want to learn the sed command in Linux examples. Then, I will revise the exact procedures with the vim editor. Today.
Exit the input mode using control + d
.
Assume we want to replace all instances of I with We. We can use the sed command, as follows.
sed 's/I/We/' file.txt
sed
is the commands
stands for string or substituteI
is the original textWe
is the new text to replace the initial one/
– we have separated the input contents with the slash,/
, delimiter
Alternatively, you can end the input with the g
. g
represents global, meaning replacing all instances of the text. Without it, the sed command replaces only the first instances of specified text per line.
Finally, we supply the command on the file.txt
file.
We love Linux. We want to learn the sed command in Linux examples. Then, We will revise the exact procedures with the vim editor. Today.
Modify the original file
If you check the contents of the initial file,
cat file.txt
you will realize that the changes did not affect it.
I love Linux. I want to learn the sed command in Linux examples. Then, I will revise the exact procedures with the vim editor. Today.
Let’s permanently replace all instances of I with We using the -i
flag.
sed -i 's/I/We/' file.txt
Replace specific instances of text
Replace at the beginning of a line
We have three instances of We.
We love Linux. We want to learn the sed command in Linux examples. Then, We will revise the exact procedures with the vim editor. Today.
We can replace We with I where We starts a line using the caret symbol, ^
.
sed 's/^We/I/' file.txt
I love Linux. I want to learn the sed command in Linux examples. Then, We will revise the exact procedures with the vim editor. Today.
Replace an independent instance
Assume we want to replace the preposition, in Linux to with Linux.
I love Linux. I want to learn the sed command in Linux examples. Then, We will revise the exact procedures with the vim editor. Today.
Let’s try changing it using the general command.
sed 's/in/with/' file.txt
The command went ahead and replaced all the first instances per line, including those in the middle of a word like Linux.
We love Lwithux. We want to learn the sed command with Linux examples. Then, We will revise the exact procedures with the vim editor. Today.
Similarly, adding the global option does not give the expected out.
sed 's/in/with/g' file.txt
We love Lwithux. We want to learn the sed command with Lwithux examples. Then, We will revise the exact procedures with the vim editor. Today.
Let’s now supply it the right option. We can do that by adding \b
before and after the word to change.
sed 's/\bin\b/with/g' file.txt
We love Linux. We want to learn the sed command with Linux examples. Then, We will revise the exact procedures with the vim editor. Today.
Delete an instance
Use the d option
We can use the d
option to delete all the lines containing a letter or a word. For example, let’s delete all the lines of file.txt
containing the letter e.
We love Linux. We want to learn the sed command in Linux examples. Then, We will revise the exact procedures with the vim editor. Today.
sed '/e/d' file.txt
Today.
The four lines were deleted except the last one that lacked the letter e.
Change the delimiter
So far, we have only used the slash, /
, delimiter. We could achieve the same output by using a space or a dot. The best situation to replace the slash /
with another delimiter like the dot .
is when the target instance contains one of the delimiters.
For example, let’s view the first ten files of the /etc
directory.
find /etc -type f | head
Say we want to replace the slashes with dots. We can do that as follows.
find /etc -type f | head > file3.txt
cat file3.txt
The output of printing the contents of file3.txt
is:
/etc/services /etc/lsb-release /etc/protocols /etc/rsyslog.conf /etc/systemd/timesyncd.conf /etc/systemd/resolved.conf /etc/systemd/journald.conf /etc/systemd/system/snap-lxd-22894.mount /etc/systemd/system/snap.lxd.activate.service /etc/systemd/system/[email protected]/disable-sshd-keygen-if-cloud-init-active.conf
Let’s now delete the /etc
part.
sed 's./etc..' file3.txt
The dots are the delimiters. /etc
is the instance to replace with a space between the last two dots.
/services /lsb-release /protocols /rsyslog.conf /systemd/timesyncd.conf /systemd/resolved.conf /systemd/journald.conf /systemd/system/snap-lxd-22894.mount /systemd/system/snap.lxd.activate.service /systemd/system/[email protected]/disable-sshd-keygen-if-cloud-init-active.conf
Conclusion
You have just learned the typical ways to apply the sed
command. Now is the time to creatively apply the command in various ways to search, find, replace, insert and delete texts.