Uses of SED command in Linux Operating system

Category : Linux/ Unix

SED is a Linux/Unix command which stands for stream editor it can perform various functions such as search, replace, delete. It also supports regular expressions which makes it useful to find different patterns. In this guide, we will take you through some of the uses of the SED command in Linux.

Add a string at the end of  each line :

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

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

Now to add any string of your choice to the end of each line we can use SED.

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

Here ‘s’ specifies the substitution operation.

This command will add Additional text to the end of each line in the file named text.txt

As a result, we get:

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

Uses of SED command in Linux

Add string at the starting of each line :

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

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

This command will add Starting text to the starting 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 a family of open source Unix-like operating systems.
Starting text Based on the Linux kernel.

sed command

Replacing text in a file :

File name test.txt which contains the following text.

Linux is a family of open-source Unix-like 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 ipsum with random in the file test.txt

As a result, we get:

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

Uses of SED command in Linux

Note: The text search is case sensitive.

These are some of the uses of SED command in Linux.

Want to know how to block and unblock IP in linux? Click the link below

How to Block and Unblock IP Address in Linux