Ubuntu

How to Use SED Command to Find and Replace String in Files

Use Sed command to Find and Replace String in Files

Introduction

Sed stands for Stream EDitor. It is a Linux command 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.

This article will explain the detail of using the Sed command to find and replace strings in files.

All of the examples in this tutorial were executed on Ubuntu 20.04.

Find and Replace

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 output will be something like this:

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

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

In which:

-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 linuxways.net testbar 789

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

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

Output:

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

Output:

As you can see, in the previous example, the substring test inside the testbar string is also placed. If you don’t want to replace a substring in the search pattern, let’s put the search pattern between two word-boundary \b:

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

Output:

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

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

Output:

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

Output:

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 ‘way’ with ‘road’ in all of the files in the current directory.

$ find . -type f -exec sed -i 's/way/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/way/road/g'

Conclusion

You’ve gone through some common examples of using the Sed command. Searching and replacing a string of text in a file with the Sed command isn’t complicated as you imagine.

If you have any concerns, please let me know in the comment section.

Similar Posts