How to replace a string in files in Linux the simple way

In this short tutorial, you will see how to replace a string in one or multiple files on Linux/Ubuntu and similar distros. We will consider simple cases only in this article.

Using rpl tool

The rpl utility can be installed from the official Ubuntu Repositories using the apt-get command as follows :

apt-get install rpl

The basic way to use rpl is to provide two strings followed by one or several filenames or folders.The first string is the one to replace while the second is the new string, .i.e the replacement string.

Read: How to use the APT command on Ubuntu/Debian Linux systems

Example use cases

One file

The following command will replace a string string_to_replace with a new one new_string in a file named file_name.txt.

rpl string_to_replace new_string file_name.txt

If the string has spaces in it, it should be embedded in quotation marks. To ignore case, you should use the -i option . For whole words, the -w option should be applied.

Multiple files

It is also possible to specify multiple files as shown below :

rpl -i -w “string to replace” “new string” file1.txt file2.txt

To indicate the extensions of the files, you should use the -x option. Recursive search in the directory can be implemented using the -R option as shown below :

rpl -x .txt -x .html -R string_to_replace new_string file*

Interactive mode using the -p can be used to search/replace as well.

For more on rpl , visit this page.

Read: How to exit Vim editor

Using sed utility

The sed tool (stream editor), is a powerful text manipulation utility which is used for text substitution. It can also carry out other text operations like deletion, insertion, search etc. The sed tool allows you to edit a whole file without having to open it. Regular expressions can also be used with sed.

The syntax is as follows :

sed ‘s/string1/string2/g’ input_file.  [linux replace string in file]       

Simply put, this will replace all occurrences of string1 with string2 in the file input_file.

The ‘/g’ option allows sed to make a global replace operation i.e. to replace all occurrences of string1. If ‘/g’ is not used, then only the first occurrence is changed.

Read: How to use grep command in Linux

Example cases of sed command in Linux

The command below will replace the word ‘building’ with the word ‘house’ in the file design.txt

sed ‘s/building/house/g’ design.txt.                [sed Linux examples]

To update the file immediately, use the -i option as follows :

sed -i ‘s/building/house/g’ design.txt

To match all different cases of the word ‘building’, add the I option as follows :

sed -i ‘s/building/house/gI’ design.txt

The following will make the replacement in all files whose name contains text

sed -i — ‘s/building/house/g’ *text*.          [sed replace sting in file]

The following will do the replacement in all files whose name ends with txt

sed -i — ‘s/building/house/g’ *.txt*

The command below will perform a recursive search and carry out the replacement for all files whose name contain ‘text’ in the current and all subdirectories :

find . -type f -name “*text*” -exec sed -i ‘s/building/house/g’ {} +

Replacement when a given context is met

In case you want to make a replacement of a word with a new one within a line that contains another word. For example, the command below will find the word ‘building’ and will replace it with ‘house’ if the corresponding line contains the word ’construction’:

sed -i -e ‘/construction/s/building/house/’ design.txt

Multiple replace operations

To replace any of word1, word2 or word3 with newword, proceed as follows :

sed -Ei ‘s/word1|word2|word3/newword/g’ file

You can also combine sed commands as follows:

sed -i ‘s/word1/newword1/g; s/word2/newword2/g; s/pete/tom/g’ file

Be aware that the replacement order matters , for instance

sed ‘s/word1/newword1/g; s/newword1/newword3/g’

will substitute word1 with newword3.


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

amin nahdy

Amin Nahdy, an aspiring software engineer and a computer geek by nature as well as an avid Ubuntu and open source user. He is interested in information technology especially Linux based ecosystem as well as Windows and MacOS. He loves to share and disseminate knowledge to others in a transparent and responsible way.

This Post Has One Comment

  1. Rhea

    how would you do this with awk? asking for a friend :’)

Leave a Reply