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

  • Instructions for using zforce command on LinuxInstructions for using zforce command on Linux
    the gzip command is a popular tool used to compress / decompress files in linux. tipsmake.com presents the basics of this tool in the article: some basic terminal commands in linux mint 11.
  • How to limit access to su command in LinuxHow to limit access to su command in Linux
    if you have added linux to your data center or are just using a single linux machine for your business, you need to make sure it is as secure as possible.
  • 12 things Linux is easier to do in the command line than graphical software12 things Linux is easier to do in the command line than graphical software
    graphical user interfaces (guis) were invented to make life easier for computer users, so it's very common to use them to perform most everyday tasks.
  • 11 df commands in Linux (with example)11 df commands in Linux (with example)
    with the df command in linux, you can easily check your system for details about disk space and file size. these 12 examples of df commands will show you how to do them in detail.
  • How to use the Linux command line on Android with TermuxHow to use the Linux command line on Android with Termux
    android is a very operating system 'capacity with more and more desktop accessibility applications. however, sometimes you want to make some things on android that can be as easy as desktop. fortunately, you can use the termux tool, which builds on the existing infrastructure and provides a command line environment that allows you to install real linux applications on your android device.
  • Instructions for using find command in LinuxInstructions for using find command in Linux
    the find command is one of the most important and handy commands on a linux system. as its name suggests, the command can find files on a linux pc based on a variety of conditions and variables you set.
  • How to use the history command in LinuxHow to use the history command in Linux
    as you spend more and more time in terminal sessions, you will constantly find new commands that make everyday tasks more efficient. the gnu history command is one of them.
  • Instructions for using pstree command on LinuxInstructions for using pstree command on Linux
    pstree is a powerful and useful command to display processes running in linux. like the ps command, it shows all the processes that are currently active on your login system. the main difference is that when running the pstree command, processes are organized into tree sorting instead of lists like using the ps command.
  • The dd command in Linux, How to use the dd commandThe dd command in Linux, How to use the dd command
    dd is a command line utility for unix-like and unix operating systems, with the main purpose of converting and copying files.
  • Use the Top command to see the processes running in LinuxUse the Top command to see the processes running in Linux
    the top command in linux is used to display all processes running in the linux environment. this tutorial shows you how to use the top command through explaining the various parameters available and the content they display.