×


Use SED Command to Find and Replace String in Files - How to perform this task on Ubuntu?

The sed stands for stream editor. It reads the given file, modifying the input as specified by a list of sed commands.

By default, the input is written to the screen, but you can force to update file.

Sed (Stream EDitor) Linux command is used for searching, finding and replacing, or inserting and deleting words and lines in a text file.

The most common use case of the Sed command is to find and replace strings in files. When using the Sed command, you can edit files without opening them.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linux Servers related queries.

In this context, we shall look into how to use the Sed command to find and replace strings in files in Ubuntu 20.04 system.


Find and Replace on Ubuntu

By default, Sed is installed in most Linux distributions. You can verify the installation of Sed on your Linux system by running:

$ sed --v

The basic syntax of using the Sed command to find and replace text is:

$ sed -i 's/Search_pattern/Replacement_pattern/g' FILE

Where:

-i: This option makes Sed command edit files in place instead of writing output to the standard output.

s: It stands for substitute.

/ /: Delimiter characters.

g: It is a Global replacement flag.

FILE: The file you want to manipulate by the Sed command.


It's highly recommended to put search pattern, replacement pattern, and other arguments in quotes.

For example, here is a text file that used for demonstration:

567 Test test test
test /bin/zsh linuxapt.com testbar 789

To replace all of the 'test' words with 'linux' words, run the following command:

$ sed -i 's/test/linux/g' test.txt

If you don't use the g flag, only the first occurrence of the search pattern in each line is replaced:

$ sed -i 's/test/linux/' test.txt

To make the search pattern match case insensitive, let's use the I flag:

$ sed -i 's/test/linux/gI' test.txt

In the Linux world, we’ll encounter many use cases that have the delimiter characters in text files. 

We have to use the backslash \ to breakout the delimiter characters. 

For example, to replace /bin/zsh with /usr/bin/bash, run:

$ sed -i 's/\/bin\/zsh/\/usr\/bin\/bash/g' test.txt


Recursive Find and Replace

In many cases, you want to recursively search directories for files that have a specific string and replace that string in all files.

For example, the following command will search and replace the string 'apt' with 'road' in all of the files in the current directory.

$ find . -type f -exec sed -i 's/apt/road/g' {} +

If the filenames contain space characters, let's use -print0 option:

$ find . -type f -print0 | xargs -0 sed -i 's/way/road/g'

To search and replace a string only on files that have a specific extension, run:

$ find . -type f -name "*.java" -print0 | xargs -0 sed -i 's/apt/road/g'


[Need urgent assistance in installing Software on Ubuntu? We are available to help you.  ]


Conclusion

This article covers how to use the Sed command on Ubuntu 20.04. Searching and replacing a string of text in a file with the Sed command isn't complicated as you imagine.

sed is a stream editor. It can perform basic text manipulation on files and input streams such as pipelines. 

With sed, you can search, find and replace, insert, and delete words and lines. 

It supports basic and extended regular expressions that allow you to match complex patterns.


To Find and replace text within a file using sed command:

The procedure to change the text in files under Linux/Unix using sed:

1. Use Stream EDitor (sed) as follows:

$ sed -i 's/old-text/new-text/g' input.txt

2. The s is the substitute command of sed for find and replace

3. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt

4. Verify that file has been updated:

more input.txt