Home » Linux Command » Linux sed command Help and Examples

Linux sed command Help and Examples

Last updated Sep 4, 2021

The Linux command SED stands for stream editor, and its use to do lot’s of operation on file for search, find and replace, insert new text, and delete after seeing the pattern.

The most common use of SED command which a System administrator or engineer doing every day is text substitution or find and replace text.

Why is SED command essential to find and replace text as we can do it in vi, vim editor quickly? Well, the answer is, the SED command do these operations easily without opening the file, which is more important when you are dealing with data in a script or within any extensive process where text replacement is a small part of your whole automated task.

SED command syntax:
sed OPTIONS... [SCRIPT] [INPUTFILE...] 

Suggested Reading: Linux sed command Structure

Examples:

We will use below created file as an input for sample command

$cat > linuxconcept.txt
Linux is most popular os. Linux is opensource and free os.
Learn Linux operating system.
which one you choose for learning Linux or UNIX.
Linux is easy to learn. Linux is a multiuser os. Learn Linux. Linux is a powerful OS.

Replacing String

Replacing or Substituting string is find string in a file and replacing that string with another one.

The below command will help you to substitute string in file “linuxconcept.txt” without open the file.

$sed 's/Linux/UNIX/' linuxconcept.txt
Output:
UNIX is most popular os. Linux is opensource and free os.
Learn UNIX operating system.
which one you choose for learning UNIX or UNIX.
UNIX is easy to learn. Linux is a multiuser os. Learn Linux. Linux is a powerful OS.

Here in the above command, “s” is indicating SED command used for substituting string, and “/” is used as delimiters. The text “Linux” is a search pattern here, and the “UNIX” is replacement string.

In default SED command only replace the first occurrence of a string in each line and not replacing the string if it comes in second, third …. times in the same line.

Replacing all the occurrence in file

To replace all find string in a file, you can use the “/g” substitute flag in command, here “g” is for “global” substitution.

$sed 's/Linux/UNIX/g' linuxconcept.txt
Output:
UNIX is most popular os. UNIX is opensource and free os.
Learn UNIX operating system.
which one you choose for learning UNIX or UNIX.
UNIX is easy to learn. UNIX is a multiuser os. Learn UNIX. UNIX is a powerful OS.

Related Posts

The 30 most useful Linux commands

This article will introduce some practical and common Linux or Unix command , which is the Linux system administrators normally use the command . This article is not a complete list, but a brief list of commands that can be useful...

15 Practical examples of tcpdump command

15 Practical examples of tcpdump command

The tcpdump is the most essential and powerful command-line network packet sniffing tool. We use it to analyze network packets, capture or filter TCP/IP packets that transferred and received over the network on a specific network interface. It is available in almost...

15 Useful “df” Commands to Check Disk Space in Linux

15 Useful “df” Commands to Check Disk Space in Linux

There are several Linux tools available on the internet to check disk space utilization; we can use any of them. But, Linux has a best built-in command-line utility called “df”. The “df” is the short form of “disk filesystem”, we can get detailed information on used...

15 Practical examples of echo command

15 Practical examples of echo command

If you are writing a bash script, you know echo command very well. It is use to display text lines on an output screen or a file. The echo command is the most common command used by all Linux users, and it is built-in Linux command. echo command syntax: echo...

Follow Us

Our Communities

More on Linux Command

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!

0 Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

two − 1 =

Shares