How to Find and Replace Text using sed Command in Linux

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.

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.

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”.

See also  10 Best Practices and Tips for Writing Shell Scripts as a Professional

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

In this article, we’ve explored how to use SED to find and replace text in files in Linux/Unix shell. SED is a powerful tool that can help automate many common text-processing tasks. By mastering SED, you’ll be able to process large amounts of text quickly and efficiently.

Leave a Comment