Home Linux Commandline Tools How to Find and Replace Text, Word, or String in File

How to Find and Replace Text, Word, or String in File

If you manage to take a deeper glimpse inside the ecosystem of the Linux operating system environment, you will discover that its built-in commands are sufficient enough to solve most of our computing problems.

One such problem is the need to find and replace text, word, or string in a file especially when you are in a server environment. A solution to this problem lets you handle nagging issues like updating the “/etc/apt/sources.list” file after a successful Linux system upgrade.

Creating the Test Text File in Linux

Create a text file with a name like “test.txt” and populate it with some text, word, or string characters. Let this file be on the same path as your terminal instance.

$ nano test.txt
OR
$ vi test.txt 
Create Text File in Linux
Create Text File in Linux

We will be using the cat command to flexible note the changes on our created test file.

$ cat test.txt
View Text File in Linux
View Text File in Linux

Using Sed to Find and Replace Text, Word, or String in a File

The power of this stream editor is in its easiness in accomplishing basic input streams transformation. The sed command offers a quick, easy, and timeless approach in finding and replacing targeted text, words, or string patterns in a file.

Find and Replace the First Occurrences of Text, Word, or String in File

From our created test file, we are going to update all the instances of the word “LinuxShellTips” with the alternative “this site”. The syntax of the sed command we will be using to accomplish this simple task is as follows:

$ sed -i 's/[THE_OLD_TERM]/[THE_NEW_TERM]/' [TARGETED_FILE] 

With reference to the above sed command usage and syntax, we can replace our test file’s “LinuxShellTips” term with “this site” term as follows:

$ sed -i 's/LinuxShellTips/this site/' test.txt

We can now use the cat command to preview the above-proposed changes to our test file to note if any substantial edits took place.

$ cat test.txt
Find and Replace Word in Linux Using Sed Command
Find and Replace Word in Linux Using Sed Command

As you can see, the first two occurrences of the word “LinuxShellTips” have been replaced with the phrase “this site”.

Find and Replace All Occurrences of Text, Word, or String in File

Despite the above command having replaced all the targeted word patterns in our test file, its usage tends to be selective in most circumstances. The command works best for small files because, in big files, only the first few occurrences of a targeted word pattern might benefit from its ‘find and replace’ prowess.

To find and replace all occurrences of a word pattern in any editable file, you should adhere to the following sed command syntax.

$ sed -i 's/[THE_OLD_TERM]/[THE_NEW_TERM]/g' [TARGETED_FILE]    

As you have noted, the 'g' in the above find & replace command syntax acts as a global variable so that all the global occurrences of a term in a targeted file are considered.

$ sed -i 's/this site/LinuxShellTips/g' test.txt
Find and Replace All Occurrences of Word in File
Find and Replace All Occurrences of Word in File

The above command finds all the global occurrences of the term “this site” and replaces it with the term “LinuxShellTips”.

Using Awk to Find and Replace Text, Word, or String in File

If you are familiar with the awk command-line utility, then you know that its powerful scripting language metrics make it a pro at text processing. Linux system administrators and professional users find this tool effective in data extraction and reporting.

The awk command syntax for a simple find-and-replace operation looks like the following snippet:

$ awk '{gsub("[THE_OLD_TERM]","[THE_NEW_TERM]"); print}' [TARGETED_FILE]

In the above syntax, awk will substitute THE_OLD_TERM from THE_NEW_TERM in the TARGETED_FILE and print the resulting file content on the system terminal.

Let us take a practical approach:

$ awk '{gsub("Linux","ubuntu"); print}' test.txt
Find and Replace Word in Linux Using Awk Command
Find and Replace Word in Linux Using Awk Command

With the dynamic functionalities of both sed and awk command tools, you should now find, replace, and overwrite text, word, or string patterns in a targeted file. These tools give a system user the needed flexibility while on a command-line interface.

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.

1 thought on “How to Find and Replace Text, Word, or String in File”

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.