How to Find and Replace Text using sed Command in Linux

Learn how to find and replace text using the sed command in Linux. Simplify text processing and automate tasks with this powerful tool.

For Linux administrators, efficiency is key. Whether managing system configurations, processing logs, or editing large text files, automation can save valuable time. One of the most powerful tools for text processing in Linux is the sed (Stream Editor) command. The sed command allows users to find and replace text in files directly from the command line, making it an essential tool for simplifying repetitive tasks.

Unlike traditional text editors, sed operates non-interactively, meaning it can process and modify files without opening them in a text editor. This makes it ideal for automation, scripting, and batch processing of large datasets. Whether you’re a beginner or an experienced Linux administrator, mastering sed can significantly improve your workflow.

In this guide, we’ll explore how to use the sed command to find and replace text in Linux, covering different use cases, syntax variations, and practical examples. By the end, you’ll be equipped with the knowledge to leverage sed for efficient text manipulation, making your daily administrative tasks much easier.

In the world of Linux/Unix shell scripting, finding and replacing text within files is a common task. One popular tool used for this purpose is SED (stream editor). In this article, we’ll explore how to find and replace text using sed command in Linux/Unix shell.

Find and Replace Text using sed


Understanding SED

SED is a stream editor that works by taking an input stream, modifying it based on a set of specified commands, and then outputting the modified stream. The basic syntax of SED is as follows:

# sed [options] 'command(s)' filename(s)

Options are optional flags that modify the behavior of SED. Commands are one or more SED commands enclosed in single quotes. Filename(s) are the file(s) to be processed.

See also  Ultimate Guide to Linux Disk Usage: Sorting Files and Directories by Size

Finding Text with SED

To find text in a file using SED, we can use the “s” (substitute) command. The basic syntax for the “s” command is as follows:

# sed 's/old_text/new_text/' filename

This will replace the first occurrence of “old_text” with “new_text” in the file “filename”. To replace all occurrences of “old_text”, we can use the “g” (global) flag:


Replacing Text with SED

To replace text in a file using SED, we can combine the “s” command with the “-i” (in-place) option. The basic syntax for replacing text with SED is as follows:

# sed -i 's/old_text/new_text/g' filename

This will replace all occurrences of “old_text” with “new_text” in the file “filename” in-place, meaning that the original file will be modified.

Using SED with Regular Expressions

One of the powerful features of SED is its ability to use regular expressions to find and replace text. Regular expressions are patterns used to match text. Here’s an example of using a regular expression to replace all occurrences of “old_text” followed by a number with “new_text”:

# sed -i 's/old_text[0-9]+/new_text/g' filename

This will replace all occurrences of “old_text” followed by a number with “new_text” in the file “filename”.


Real life example :

This example will show how to find and replace text using sed command. We take /etc/host file as an example. We will replace any text “linodelinux.com” to “linodelinux.local” in /etc/hosts

# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.8.111   www.linodelinux.com
192.168.8.111   website1.linodelinux.com
192.168.8.111   website2.linodelinux.com

Just issue this command :

# sed -i 's/originaltext/newtext/g' file.txt

Example :

# sed -i 's/linodelinux.com/linodelinux.local/g' /etc/hosts

New /etc/host file as below :

# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.8.111   www.linodelinux.local
192.168.8.111   website1.linodelinux.local
192.168.8.111   website2.linodelinux.local

Conclusion

Mastering the sed command in Linux can greatly enhance your efficiency as a system administrator. Whether you need to modify configuration files, clean up logs, or automate text replacements, sed provides a fast, flexible, and scriptable solution. By understanding its syntax and powerful search-and-replace capabilities, you can streamline daily tasks and minimize manual effort.

See also  How to Get the Current Directory in Python

As you continue to explore sed, consider integrating it into your shell scripts for even greater automation. Pairing sed with other command-line tools like awk, grep, and find can further enhance your ability to manipulate text efficiently. With practice, sed will become an indispensable part of your Linux toolkit, helping you work smarter and more productively.

Now that you’ve learned the basics, try experimenting with different sed options and test its capabilities on real-world tasks.


Leave a Comment