How to use sed command in Linux

Sed stands for 'Stream Editor' and it is a powerful utility that allows you to parse and convert text right from the command line. Whether you are dealing with configuration files, scripts, or even plain text, sed is the tool that helps you manipulate text quickly and efficiently.

The main use of sed is to search for specific patterns of text and replace them with something else. It can also delete or insert lines and perform other text conversions. It is especially useful for batch editing files or working in shell scripts to automate various tasks.

Although sed itself is extremely versatile, it is often used in combination with other Linux commands such as awk for text processing, grep for pattern searching, and cat for displaying file contents. Together, these tools form a powerful toolkit for text processing in a Linux environment.

General syntax for sed command:

$ sed [OPTIONS] [FILE].

1. Replace text

echo "Text" | sed 's/Replaceable_Word/The_Word_That_Replaces/'

Use the sed command to search and replace any part of text. 's' represents a search and replace action.

For example, let's say you have the string 'I love CSS' and you want to replace 'CSS' with 'CSS Libraries'.

echo "I love CSS" | sed 's/CSS/CSS Libraries/'
I love CSS Libraries

In this example, the echo command outputs 'I love CSS' , then sed replaces 'CSS' with 'CSS Libraries' . The end result is 'I love CSS Libraries' .

2. Replace text on a specific line in the file

sed '[line] s/harder/easier/g' [file]

The 'g' option of the sed command is used to replace anything that matches the pattern.

For example, suppose you have a text file named example.txt with the following content:

Life is hard. Working harder is the key to success. The harder you work, the luckier you get.

To replace all occurrences of the word 'harder' with 'easier' on line 2 of example.txt, you would run:

sed '2 s/harder/easier/g' example.txt

After running the command, the result displayed on the terminal will be:

Life is hard. Working easier is the key to success. The harder you work, the luckier you get.

Note that the word 'harder' is replaced by 'easier' only in line 2.

If you want to save these changes to a file, you can use the -i option :

sed -i '2 s/harder/easier/g' example.txt

After running this command, the contents of example.txt will be permanently changed to:

Life is hard. Working easier is the key to success. The harder you work, the luckier you get.

3. Replace the first match with new text

sed 's/harder/easier/' [file]

This command replaces only the first match of the search pattern.

For example, suppose you have a text file named example.txt with the following content:

Life is harder than we think. Working harder is the key to success. No pain, no gain. Work harder.

You can use the sed command to replace the word 'harder' with 'easier' in each line:

sed 's/harder/easier/' example.txt

After running the command, the result will be:

Life is easier than we think. Working easier is the key to success. No pain, no gain. Work easier.

4. Delete matching lines

sed '/Something/d' example.txt

Use the d option of the sed command to delete any line from the file.

For example, suppose you have a file named example.txt with the following content:

Hello World Something is here Another line Yet another line Something else

Running the command sed '/Something/d' example.txtwill output:

Hello World Another line Yet another line

5. Search for case-insensitive word + delete it

sed '/Sample/Id' example.txt

The I option of the sed command is used to search for a matching pattern in a case-insensitive manner.

 

For example, suppose you have a file named exampleb.txt with the following content:

This is a Sample line. Another line. Yet another Sample line. Final line.

Running the command sed '/Sample/Id' example.txtwill produce the following output:

Another line. Final line.

6. Replace words with capital letters

sed 's/(libraries)/U1/Ig' example.txt

Use the U option of the sed command to convert any text to uppercase.

For example, suppose you have a file named example.txt with the following content:

I love libraries. libraries are great. You can find many books in libraries.

After running the sed command, the result will be:

I love LIBRARIES. LIBRARIES are great. You can find many books in LIBRARIES.

7. Replace words with lowercase letters

sed 's/(libraries)/L1/Ig' example.txt

The L option of the sed command is used to convert any text to lowercase.

For example, suppose you have a file named example.txt with the following content:

Libraries are essential for research. libraries help in many ways. I love LIBRARIES!

After running the sed command, the result will be:

libraries are essential for research. libraries help in many ways. I love libraries!

8. Insert blank lines into the file

sed G [file]

Use the G option of the sed command to insert a blank line after each line of the file.

For example, suppose you have a file named example.txt with the following content:

Hello World This Is A Test

You can run the following command to add a new line to the end of each line:

sed G example.txt

After running the command, the result will be:

Hello World This Is A Test

9. Print the line number of the file

sed '=' [file]

The = sign is used to print the line number before each line of text in a file.

For example, suppose you have a file named example.txt with the following content:

Hello World This Is A Test

You can run the following command to print the line number before each line:

sed '=' example.txt
1 Hello 2 World 3 This 4 Is 5 A 6 Test
4 ★ | 1 Vote

May be interested

  • 5 reasons why people love the Linux command linePhoto of 5 reasons why people love the Linux command line
    many people are afraid of the command line. they see it as something for software developers or geeks. but the command line is merely another way of interacting with a pc, and there are some tasks that are very easy to do with the cli.
  • How to install Grafana on Alma LinuxPhoto of How to install Grafana on Alma Linux
    grafana is a free, open source monitoring and data visualization software. by default, it supports graphite, elasticsearch, prometheus and other databases.
  • How to use the Ping command in LinuxPhoto of How to use the Ping command in Linux
    ping serves as an important diagnostic tool for network engineers, system administrators, and even curious technology enthusiasts.
  • How to Customize Linux TerminalPhoto of How to Customize Linux Terminal
    it seems like no matter what distribution you use, the terminal emulator is the most boring thing. the good thing you can do is make it look a little better with lots of customization options.
  • What is Gentoo Linux? How to install?Photo of What is Gentoo Linux? How to install?
    gentoo linux is a powerful and scalable distribution that sticks to the original source-based package management mechanism.
  • How to use ncdu to check disk space in UbuntuPhoto of How to use ncdu to check disk space in Ubuntu
    have you ever found yourself stuck trying to figure out the disk space consumption on your ubuntu system?