Home Linux Commands How to Use Sed to Find and Replace String in Files

How to Use Sed to Find and Replace String in Files

Sed (short for Stream Editor) is a command-line utility in GNU/Linux operating systems used to modify text files. It can be considered as a command-line text editor; however, it is different from ‘real-time’ text editors like Vim and Nano, in the sense that the latter two are interactive editors whereas sed is based solely on invoking the command with required arguments.

[ You might also like: How to Remove Lines from a File Using Sed Command ]

Today, we will learn how to use the sed command to find and replace a string in a text file in Linux.

Find and Replace String in Files

A sed command is available by default in GNU/Linux distributions and we will be using the following sample text file to test sed, but before that, view the contents of the file using the cat command as shown.

$ cat test.txt
View File Contents
View File Contents

Let’s use sed to search for the string ‘Ubntu‘ in this file and replace it with ‘Ubuntu‘. Following is the basic syntax of sed to search and replace a string in a file.

$ sed 's/string_tobe_replaced/new_string/g' filename

Let’s try to understand the syntax of the first argument here. The option 's' at the beginning specifies to sed the internal command for string search and replace. Sed is a complex tool and has many such internal commands to manipulate a file in various ways.

[ You might also like: How to Find Files Containing Specific Text String in Linux ]

Then we have the original and the new string separated by backslashes. Finally, we have a 'g' which specifies that the string should be replaced ‘Globally’, i.e., all over the file.

If 'g' is not specified, it will replace only the first occurrence of the string in each line of the file.

$ sed 's/Ubntu/Ubuntu/g' test.txt
Find and Replace String in File
Find and Replace String in File

As you can see above, sed has successfully replaced ‘Ubntu’ with ‘Ubuntu’ and written the file contents with replaced string on the standard output.

This is the default behavior of sed: to write the new contents of the file on standard output. To write the contents to the file itself, use the '-i' argument.

$ sed -i 's/Ubntu/Ubuntu/g' test.txt

However, it is not recommended to write to the original file itself without backing it up; especially if it is a large file because if there was an error in the new string, the user again needs to spend time fixing it.

Instead, call the argument '-i' with an extension, for example ‘-i.bkp’. What this will do is: it will create a backup file of the original ‘test.txt’ called ‘test.txt.bkp’, so that if the original has any errors after string replacement, the previous version can be restored from the backup.

$ sed -i.bkp 's/Ubntu/Ubuntu/g' test.txt
Search and Replace String in File
Search and Replace String in File

As you can clearly see, the backup file has original contents, whereas string occurrences in the original file have been replaced. Hence if there arises a need to restore the original file, the user can simply run:

$ mv test.txt.bkp test.txt
Conclusion

In this article, we saw a simple use case of sed: to find and replace a string in a file. This feature comes in handy especially when used in a script; the original and new strings can be then specified with shell variables. With interactive editors, there is usually no way to automate the search and replace functionality.

Thanks a lot for reading and let us know your thoughts below!

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.