Recursive Search and Replace in Text Files in Linux


One of most basic and frequently performed tasks in any operating system is search and replace. This becomes even more crucial when working with text files, where multiple instances of same word or phrase may be scattered throughout document. In such cases, manually editing each instance can be both time-consuming and error-prone. In this scenario, a tool like Recursive Search and Replace in Linux comes in handy.

In Linux, Recursive Search and Replace can be done via various methods. most common one involves use of command-line tools like Sed, Awk, and Perl. These tools are efficient, lightweight, and have been around for decades, making them a reliable choice for text editing. In this article, we will discuss how to perform Recursive Search and Replace using these tools.

What is Recursive Search and Replace?

Recursive Search and Replace refers to process of searching for a particular word or phrase in multiple files or directories and then replacing it with another word or phrase. It is called "recursive" because process is repeated for each subdirectory of parent directory until all instances of word or phrase are replaced. This is useful when dealing with large text files or directories containing multiple files, as it saves time and effort.

How to Perform Recursive Search and Replace in Linux

There are different ways to perform Recursive Search and Replace in Linux. In this article, we will cover most commonly used methods using Sed, Awk, and Perl.

Using Sed

Sed (Stream Editor) is a powerful command-line tool used for editing text files. It can be used to perform various text manipulation tasks, including Recursive Search and Replace. basic syntax for using Sed is as follows −

sed 's/old_text/new_text/g' file_name

The "s" command stands for "substitute," which is followed by old text, new text, and file name. "g" option stands for global, which means that all instances of old text in file will be replaced with new text.

To perform Recursive Search and Replace using Sed, we need to use "-r" option, which enables extended regular expressions, and "-i" option, which enables in-place editing. syntax for using Sed for Recursive Search and Replace is as follows −

find . -type f -name '*.txt' -exec sed -i -r 's/old_text/new_text/g' {} +

The "find" command is used to locate files and directories, and "-type f" option is used to find only files. "-name" option is used to specify file extension, which in this case is "*.txt". "-exec" option is used to execute a command on each file found. command is then passed to Sed, which performs Recursive Search and Replace.

Example

Suppose we have a directory called "test" containing two subdirectories, "subdir1" and "subdir2," each containing a file called "file.txt". content of "file.txt" in "subdir1" is as follows −

The quick brown fox jumps over lazy dog.
The quick brown fox is a common phrase in English.

The content of "file.txt" in "subdir2" is as follows −

The quick brown fox is a common phrase in English.
The quick brown fox is also a song.

To perform Recursive Search and Replace using Sed, we need to run following command −

find test/ -type f -name '*.txt' -exec sed -i -r 's/quick/brown/g' {} +

This command will replace all instances of word "quick" with "brown" in all ".txt" files found in "test" directory and its subdirectories. result will be as follows −

Content of "file.txt" in "subdir1" after replacing −

The brown brown fox jumps over lazy dog.
The brown brown fox is a common phrase in English.

Content of "file.txt" in "subdir2" after replacing −

The brown brown fox is a common phrase in English.
The brown brown fox is also a song.

Using Awk

Awk is another powerful command-line tool used for text processing. It is especially useful for working with structured text files. basic syntax for using Awk is as follows −

awk '{gsub(/old_text/, "new_text"); print}' file_name

The "gsub" function stands for "global substitute," which is followed by old text, new text, and file name. "print" function is used to print modified text to console.

To perform Recursive Search and Replace using Awk, we need to use same "find" command as in Sed example. syntax for using Awk for Recursive Search and Replace is as follows −

find . -type f -name '*.txt' -exec awk '{gsub(/old_text/, "new_text"); print > FILENAME}' {} +

The "FILENAME" variable is used to indicate current file being processed. modified text is then redirected back to file using ">" operator.

Example

Using same directory structure and file content as in Sed example, we can perform Recursive Search and Replace using Awk by running following command −

find test/ -type f -name '*.txt' -exec awk '{gsub(/quick/, "brown"); print > FILENAME}' {} +

This command will replace all instances of word "quick" with "brown" in all ".txt" files found in "test" directory and its subdirectories. result will be same as in Sed example.

Using Perl

Perl (Practical Extraction and Reporting Language) is another powerful scripting language used for text processing. It is widely used for web development, system administration, and network programming. basic syntax for using Perl is as follows −

perl -pi -e 's/old_text/new_text/g' file_name

The "-pi" option enables in-place editing, and "-e" option is used to specify a script to be executed. "s" function stands for "substitute," which is followed by old text, new text, and file name. "g" option stands for global, which means that all instances of old text in file will be replaced with new text.

To perform Recursive Search and Replace using Perl, we need to use same "find" command as in Sed and Awk examples. syntax for using Perl for Recursive Search and Replace is as follows −

find . -type f -name '*.txt' -exec perl -pi -e 's/old_text/new_text/g' {} +

Example

Using same directory structure and file content as in Sed and Awk examples, we can perform Recursive Search and Replace using Perl by running following command −

find test/ -type f -name '*.txt' -exec perl -pi -e 's/quick/brown/g' {} +

This command will replace all instances of word "quick" with "brown" in all ".txt" files found in "test" directory and its subdirectories. result will be same as in Sed and Awk examples.

Conclusion

In this article, we have explored three different command-line tools for performing Recursive Search and Replace in Text Files in Linux - Sed, Awk, and Perl. Each of these tools has its own unique syntax and features, but all are very powerful and efficient for text processing.

The ability to perform Recursive Search and Replace in Text Files is an essential skill for any Linux system administrator, programmer, or power user. Whether you are working on a small project or a large-scale deployment, ability to quickly and easily modify text files can be a huge time-saver.

By mastering these tools and techniques, you can become much more productive and efficient in your work, and save yourself a lot of time and hassle in process. So, go ahead and try them out for yourself, and see how much easier your work becomes!

Updated on: 14-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements