SED command usage in Linux with examples

In this guide, we shall show you different uses of SED command besides some useful tips and tricks that should help you in your daily Linux use.

SED or Stream Editor command is one of the must-know commands for any Linux user while working in the Terminal.

SED command helps you in performing various tasks on a text file such as updating, deleting, searching and inserting text. One of the strong features is it supports using regular expressions. Also, it lets you edit files without even opening the file.

In this guide, we will show you different uses of sed command besides some useful tips and tricks that should help you in your daily Linux use. For this tutorial, we are using Ubuntu as our Linux machine but you can use the below tutorial on any Linux distribution.

First, let’s make sure that your system is up-to-date using the following commands:

sudo apt update
sudo apt upgrade

SED command usage with examples

Syntax

sed OPTIONS... [SCRIPT] [INPUTFILE...]

Before starting with the commands, let’s ensure that SED is already installed on your computer.

sed --version

Get Sed Command Version
Get Sed Command Version

As shown in the above screenshot, you should also see the version in your Terminal.

Let’s create a new text file that we will use to test our examples on it.

vi fosslinux_sed

Here is the text in the file so you can use it to test yourself.

1. hi theree
2. Sed or Stream Editor command is considered one of the Linux well-known commands that you must know.
3. Using the sed command will help you in performing various tasks on a text file like updating, deleting, searching and inserting text.
4. One of the sed important features is that it supports using regular expressions.
5. Also, the sed command can be used to edit files without even opening it.
6. In this guide, we will show you different uses of sed command besides some useful tips and tricks that will help you in your daily Linux use.
7. For this tutorial, we are using Ubuntu as our Linux machine but you can use the below sed commands in any Linux distribution.

Create a New Example File
Create a New Example File

Example 1. Replace all the occurrence of the “sed” word with “SEEEEEEED”. Notice that its case sensitive.

sed 's/sed/SEEEEEEED/g' fosslinux_sed

Replace Certain Word
Replace Certain Word

Example 2. Replace all the occurrence of the “sed” word with “SEEEEEEED” except for the “sed” word in the fourth line.

sed '4!s/sed/SEEEEEEED/' fosslinux_sed

Replace All the Occurence Except for the 4th Line
Replace All the Occurrence Except for the 4th Line

Example 3. Add space at the beginning of each line then redirect the output to a new text file.

sed 's/^/ /' fosslinux_sed > new_fosslinux_sed

Add Spaces And Redirect Output to New File.
Add Spaces And Redirect Output to New File.

Now let’s display two files and watch the difference.

Preview Difference of Two Files
Preview Difference of Two Files

Example 4. Preview all the text between the “Sed” word and the “expressions” word.

sed -n '/Sed/,/expressions/p' fosslinux_sed

Display Text Between Two Words
Display Text Between Two Words

Example 5. Preview all the text between the 2nd and 5th lines.

sed -n '2,5p' fosslinux_sed

Display Text Between Two Line Numbers
Display Text Between Two Line Numbers

Example 6. Preview all the text except the text between the 2nd and 5th lines.

sed '2,5d' fosslinux_sed

Display Text Not Between Two Line Numbers
Display Text Not Between Two Line Numbers

Example 7. Now double all the spaces within the file.

sed G fosslinux_sed

Double All Spaces in File
Double All Spaces in File

Example 8. Remove the last line from the file.

sed '$d' fosslinux_sed

Delete the Last Line in the File
Delete the Last Line in the File

Example 9. Replace the “Linux” word with “Ubuntu” in case “Unix” is not found.

sed '/Unix/!s/Linux/Ubuntu/' fosslinux_sed

Replace Word in case Strig Not Found
Replace Word in case Strig Not Found

Example 10. Remove all the text between the 2nd line and the line that has the “regular” word.

sed '2, /regular /d' fosslinux_sed

Remove Text From the 2nd Line to Lline has the Word regular
Remove Text From the 2nd Line to Line has the Word regular

Example 11. Remove all spaces from the beginning of each line.

sed 's/^[ ^t]*//' new_fosslinux_sed

Remove Spaces From The Begining of each Line
Remove Spaces From The Begining of each Line

Example 12. Remove all spaces from the end of each line.

sed 's/[ ^t]*$//' new_fosslinux_sed

Remove Spaces From The End Of each Line
Remove Spaces From The End Of each Line

Example 13. Remove all spaces from the beginning and the end of each line.

sed 's/^[ ^t]*//;s/[ ^]*$//' new_fosslinux_sed

Remove Spaces From The Begining and End of each Line
Remove Spaces From The Begining and End of each Line

Example 14. Replace string that is found in the 1st instance only in a line.

sed 's/2/2222/' fosslinux_sed

Replace String Found At the Line Start Only
Replace String Found At the Line Start Only

Example 15. Replace string that is found in the 3rd instance only in a line.

sed 's/e/eeeeeee/' fosslinux_sed

Replace String Found in the 3rd Instance in a Line Only
Replace String Found in the 3rd Instance in a Line Only

Example 16. Replace string that is found in all instances in a line.

sed 's/e/eeeeeee/g' fosslinux_sed

Replace String Found in a Line
Replace String Found in a Line

Example 17. Replace string if only a certain word is found.

sed '/sed/s/a/aaaa/g' fosslinux_sed

Replace String If a Certain Word is Found
Replace String If a Certain Word is Found

That’s it for now. I hope you have enjoyed the sed command usage.

Hend Adel
Hi! I'm Hend Adel, a freelancer technical geek with successful experience in Database, Linux and many other IT fields. I help to build solutions to suit business needs and creating streamlined processes. I love Linux and I'm here to share my skills via FOSS Linux! Thanks for reading my article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,538FansLike
365FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to install CouchPotato on Ubuntu

Downloading movies and copying them over to your home server can get frustrating, especially if you are doing it daily! What if you have an option to download videos automatically, quickly, and above all with excellent quality. CouchPotato allows you to download movies easily once they are available and released automatically.

Top 20 Rsync Examples in Linux

The Rsync (remote sync) command is a Linux/Unix utility used to synchronize and copy files and directories either locally or remotely. Most Linux geeks use Rsync to mirror, backup or migrate data across folders, across disks and networks. One notable feature with the Rsync command is that it uses the "delta transfer algorithm."

Setting up NFS Server on Ubuntu Server

We have put together a detailed step-by-step tutorial that will guide you on how to install and set up NFS Server on Ubuntu so you too can start sharing data with other client systems. It is divided into sections, the first part discussing how to set up the host server and the second part showing you how to set up the client-side. For this tutorial, we will be using Ubuntu 20.04 LTS.

Crontab in Linux Explained With Examples

Crontab is the Linux equivalent of the Window's Task Scheduler. It can help you set up a task to run automatically at a regular...

The 10 Best Linux Server Distros For Home And Businesses

By the year 2020, it is estimated that there are close to 600 Linux distributions in the market. It includes both servers and Desktop versions; therefore, if you are looking for lightweight Linux distribution for your old PC or a reliable desktop version for employees in your organization, you may be overwhelmed with the number of choices for finding one for your use.

How to list the installed Packages on Ubuntu

It is only natural to start installing more and more software on your Ubuntu PC once you start using it. Similarly, it is also natural to lose track of the different packages that you have installed on your system. Eventually, you may be overwhelmed with the number of packages in your system and with the ones you hardly used.

MUST READ

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

How to make Ubuntu look like Windows XP

If you are in a nostalgic mood and would like to make your Ubuntu look like Windows XP, you just landed in the right place. In this article, we will show you how you can make Ubuntu look like Windows XP by applying the GTK+ theme. Our Ubuntu distribution of choice is Ubuntu 20.04 LTS.

5 Best Notepad++ Alternatives for Linux

Notepad++ has been the de facto standard for source code editors for nearly 16 years, almost since its creation in 2003.  For Windows users, that is.  For years, Linux users had no source code editor that compared to Notepad++ with all its bells and whistles, such as code folding, scripting, markup languages, syntax highlighting, auto-completion for programming (limited).

5 ways to send emails using the command-line in Linux

Did you master using the command-line in Linux? There is no limit to what one can do via the Linux Terminal. One of such things we are going to discuss today are methods of sending an email using the command-line.

6 Best Linux Distros for Laptops

Whether buying a Linux pre-installed laptop or selecting a Linux distro for your existing laptop, there are many things to consider. Let's take you through some of the best Linux distros that are optimized for Laptops in this 2019 edition of the article. Read on.