SED command usage in Linux with examples

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.

Leave a comment

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