Sed Command in Linux Examples

Sed Command in Linux Examples

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.

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.

Input
sed 's/I/We/' file.txt
  • sed is the command
  • s stands for string or substitute
  • I is the original text
  • We 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.

Output
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 1

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

sed 2

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, ^.

Input
sed 's/^We/I/' file.txt
Output
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.

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

Unexpected output ~1:
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
Unexpected output ~2
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
Expected output
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.

sed 3

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.
Input
sed '/e/d' file.txt
Output
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.

Store the file path copies in a new file
find /etc -type f | head > file3.txt
Print result of 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.

Input
sed 's./etc..' file3.txt

The dots are the delimiters. /etc is the instance to replace with a space between the last two dots.

Output
/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.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like