SED command

SED command in the Linux Operating system

Today we will learn the SED command in the Linux Operating system. SED is a Linux/Unix command that stands for stream editor that can perform various functions like search, replace and delete. It also supports regular expressions which makes it useful for finding different patterns. In this guide, we’ll walk you through some uses of the SED command in Linux.

Add a string to the end of each line:

We have a file called test.txt which contains the following text.

Linux is an open source Unix-like family of operating systems.
Based on the Linux kernel.
First released on September 17, 1991.

Now we can use SED to append any string we like to the end of each line.

Example :

sed 's/$/inserted/' -i test.txt

Here ‘s’ specifies the substitution operation.

This command will add extra text at the end of each line in the file named text.txt

As a result, we get:

 

Linux is an open source Unix-like family of operating systems. inserted
Based on the Linux kernel.inserted
First released on September 17, 1991.inserted

Add the string to the beginning of each line:

We have a file called test.txt which contains the following text.

Linux is an open source Unix-like family of operating systems.
Based on the Linux kernel.

This command will add Starting text to the beginning of each line in the file named text.txt

Example:

sed 's/^/Starting text /' test.txt

As a result, we get:

Starting text Linux is an open source Unix-like family of operating systems.
Starting text Based on the Linux kernel.

Replace text in a file:

The file name is test.txt which contains the following text.

Linux is an open source Unix-like family of operating systems.
Based on the Linux kernel.
First released on September 17, 1991.

Example :

sed 's/Linux/new linux/' test.txt

This command will replace Epsom with random in the test.txt file

As a result, we get:

 

new Linux is an open source Unix-like family of operating systems.
Based on the new linux kernel.
First released on September 17, 1991.

Note: Text search is case-sensitive.

These are some uses of SED commands in Linux. Learn Linux kill Command. Buy Linux VPS from Oudel Inc.

Scroll to Top