Linux SED Command: Everything you Need to Know

What is Linux SED Command?

In this tutorial, we will explain the Linux SED command using some real examples. SED (Stream Editor) is one of the most used Linux commands in scripts and command lines. It searches, replaces, inserts, and deletes strings. The most common use for the SED command is to find and replace a string in a file without opening it, saving time. System administrators regularly use this command while writing scripts and automating tasks on the server.

In the next paragraphs, we will teach you everything you need to know about the SED command. Let’s get started!

Prerequisites

  • A Linux VPS OS, such as Ubuntu, Debian, or AlmaLinux
  • User privileges: root or non-root user with sudo privileges

Syntax of the SED command

The basic syntax of the SED command is the following one:

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

Create txt file

Let’s first one file; we will use that for all examples in the next paragraphs. Create one txt file:

sudo nano test.txt

Paste the following lines of code:

Three Two One and here we go..

Ubuntu is number One is fast and killing like gun, admins are using it for fun.

Debian is number Two and is used by you, if something messy you do not have clue.

AlmaLinux is number three that has to be you can never use it if you dont Linux admin degree.

Replace a string in a file

This example is for replacing a specific string in the whole .txt file. To replace the string “number” with the word “digit” for example you need to execute the following command:

sed 's/number/digit/g' test.txt

After executing this command, you will get the output like this:

root@host:~# sed 's/number/digit/g' test.txt
Three Two One and here we go..

Ubuntu is digit One is fast and killing like gun, admins are using it for fun.

Debian is digit Two and is used by you, if something messy you do not have clue.

AlmaLinux is digit three that has to be you can never use it if you dont Linux admin degree.

Replace string in a specific line

To replace the string “is” in the third line with “was” execute the following command:

sed '3 s/is/was/' test.txt

You should get the following output:

root@host:~# sed '3 s/is/was/' test.txt
Three Two One and here we go..

Ubuntu was number One is fast and killing like gun, admins are using it for fun.

Debian is number Two and is used by you, if someting mess you do not have clue.

AlmaLinux is number three that has to be you can never use it if you dont Linux admin degree.

Replace a specific occurrence in the line of the file

To replace a string in some specific occurrence in the line of the file, for example, the word “is” in the second occurrence to “was” you need to execute the following command:

sed 's/is/was/2' test.txt

You will notice in the second and the third lines the word “is” is changed to was in the second occurrence

root@host:~# sed 's/is/was/2' test.txt
Three Two One and here we go..

Ubuntu is number One was fast and killing like gun, admins are using it for fun.

Debian is number Two and was used by you, if something messy you do not have clue.

AlmaLinux is number three that has to be you can never use it if you dont Linux admin degree.
root@host:~# 

Print lines only with the substituted text

As you can see, the output was the whole text after the change. If we want to see only the changed lines we need to use the following command:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
sed -n 's/is/was/2p' test.txt

You should see only the lines with changes:

root@host:~# sed -n 's/is/was/2p' test.txt
Ubuntu is number One was fast and killing like gun, admins are using it for fun.
Debian is number Two and was used by you, if something messy you do not have clue.

Replace string with Ignore Case

To replace a specific string, whether it is with the lower or upper case, you need to use the i flag in the command. Let’s replace the word “two” with the word “silver”:

sed -n 's/two/silver/ip' test.txt

You should get only the changed lines:

root@host:~# sed -n 's/two/silver/ip' test.txt
Three silver One and here we go..
Debian is number silver and is used by you, if someting mess you do not have clue.

About SED command

If you want to know in detail about the SED command, you can execute the command below:

man sed

You will get a full explanation of the command, options, synopsis, etc

SED(1) User Commands SED(1)

NAME
       sed - stream editor for filtering and transforming text

SYNOPSIS
       sed [-V] [--version] [--help] [-n] [--quiet] [--silent]
           [-l N] [--line-length=N] [-u] [--unbuffered]
           [-E] [-r] [--regexp-extended]
           [-e script] [--expression=script]
           [-f script-file] [--file=script-file]

That was it. You learned some basic and important SED commands on Linux OS. Of course, if you have an issue with a command, you can always contact our technical support, and our admins will help you immediately. We are available 24/7.

If you liked this post about the SED command, please share it with your friends or leave a comment down below.

Leave a Comment