How to Find and Replace String in Bash [5 Methods]

Find and replace is a fundamental task that is needed for changing things in files. Fortunately, Bash offers multiple alternative ways to find and replace. Some people like using the built-in nano text editor. Others prefer to directly replace text using Bash commands like sed. No matter what you like, you’ll find the answers to queries regarding replacing the content of a file in this reading.

Key Takeaways

  • Find and replace in nano editor.
  • Find and replace using the sed command.
  • Find and replace using the awk command.
  • Find and replace text across multiple files at a time.

Free Downloads

5 Methods to Find and Replace String in a File Using Bash Script

In the following demonstrations, I’ll explain how to find and replace text using both the nano editor and the sed command. Moreover, the demonstration includes replacing text in multiple files at a time.

You can read the Comparative Analysis of Methods to find one that suits you.

Method 1: Find and Replace Text in the Nano Editor

Suppose you have a file and you want to replace certain text or words with new text from the file in the basic nano editor. Follow the steps below to successfully find and replace text in the built-in nano editor in Bash.

❶ At first, launch an Ubuntu Terminal.

❷ First of all, open the file in the nano editor. I am opening find_replace.sh file to demonstrate this.

nano find_replace.sh
EXPLANATION
  • nano: Opens a file in the Nano text editor.
  • find_replace.sh: Name of the file.

Open a file in nano editor❸ After opening the file you can replace any text you want using CTRL+\. In my file, I want to replace the word “hidden” with “protected” if there is any.Selecting Replace in nano editor to find and replace text

Search the word to replace in Nano❹ After selecting, a bottom menu appears as follows. Type the word or text you want to replace and press ENTER. I searched for the word “hidden” to replace it. This will automatically perform a case-insensitive search of the given text.

NOTE: To perform a case-sensitive search enable it by pressing Meta(OS key)+C.

❺ After pressing the ENTER key the bottom menu will ask you to put the new text. I inserted “protected” to replace the word “hidden” and pressed ENTER.Inserting the new text to replace in Nano❻ After pressing the ENTER, another menu appears at the bottom and asks you whether you want to replace the matched instance only. You can replace all instances of the given text by pressing A. In my case, I press A to replace all instances of the word “hidden”.Pressing A to replace in all occurrences in nano❼ Once you press A for All or Y for Yes, this will instantly replace the old text with new text. As I pressed A, I got a pop-up message stating that in how many occurrences the word “hidden” is replaced.Message after find-and-replace operation in nano As you can see the word “hidden” is replaced by “protected” in all four occurrences.

Method 2: Find and Replace Text in File Using the ‘sed’ Command in Bash

The sed command is used to find and replace certain strings in a file. The -i option of the sed command is used to replace a string in place where it matches the search. The basic syntax of the sed command to find and replace string is below:

sed -i s”/old_string/new_string/” filename

Steps to Follow >

❶ Write the following command to open a file named replace.sh in the build-in nano editor:

nano replacefile.sh
EXPLANATION
  • nano: Opens a file in the Nano text editor.
  • replace.sh: Name of the file.

Creating a file in Nano➋ Copy the following scripts and paste them into nano. Press CTRL+O and ENTER to save the file; CTRL+X to exit. Alternatively, copy the following script. Paste the script into a text editor and save it as .sh file.

Script (replace.sh) >

#!/bin/bash

# Specify the path to the config file
config_file="/etc/ssmtp/ssmtp.conf"
# Ask user for the new mailhub value
read -p "Enter the new mailhub value: " new_mailhub
# Check if the config file exists

if [ -f "$config_file" ]; then
# Perform find and replace using sed
sed -i "s/^mailhub=.*/mailhub=$new_mailhub/" "$config_file"
echo "Replacement done in $config_file"
else

echo "$config_file does not exist."
fi
EXPLANATION

The line #!/bin/bash is known as a shebang and is used at the beginning of a script file to specify the Bash interpreter. This Bash script allows users to modify the “mailhub” value in a configuration file located at “/etc/ssmtp/ssmtp.conf“. The script checks if the configuration file exists and, if so, uses the sed command to locate lines starting with “mailhub=” and replaces the value portion with the user-provided new value. The -i option enables in-place editing. Upon successful replacement, the script confirms the change with a message using the echo command.

➌ Use the following command to make the file executable:

chmod u+x replace.sh

Changing permission of Bash script file

EXPLANATION
  • chmod: Changes permissions.
  • u+x: Giving the owner executing permission.
  • replace.sh: Name of the script.

➍ See the content of the configuration file using the command below.

sudo cat /etc/ssmtp/ssmtp.conf

Previous value of mailhub before replacing text➎ Now, run the replace.sh script by the following command:

sudo ./replace.sh

Exexuting Bash script to find and replace using sed commandThe program displays a message that it successfully changed the mailhub value to Email which was previously mail.

➏ Once executed the script changes the configuration file’s mailhub value from “mail” to “Email“.

Let’s see the configuration file again.Finding and replacing text using sed command As you can see the value of mailhub has been changed to Email.

NOTE: In this process, you can replace the old text for the first occurrence only. If you want to change all the occurrences then add a ‘/g’ at the end of the search. It means you want to replace the text globally.

The syntax of the command will be as follows:

sed -i 's/old_text/new_text/g' file_name

Method 3: Find and Replace Text Using the ‘awk’ Command in Bash

One can use the awk command to replace the text of a file. But this command can not perform in-place replacement. The basic syntax of the find-and-replace operation of the awk command is as follows:

awk '{sub(/old_text/,new_text); print}' filename

This will replace the first occurrence of old_text only. To replace all the occurrences use gsub instead of the sub function within the awk command.

You can follow the Steps of Method 2 to learn about creating and saving shell scripts.

Script (awkreplace.sh) >

#!/bin/bash

read -p "New Email password:\n" -s new_pass
awk -v new_pass="$new_pass" '/^Email : / {sub($3, new_pass)} 1' password.txt
mv temp.txt password.txt
cat password.txt
EXPLANATION

This Bash script prompts the user to store a new email password in a secure manner using the -s flag with the read command. It then utilizes the awk command to search for lines starting with “Email : ” in a file named password.txt.

When such a line is found, it substitutes the third field (the existing password) with the newly entered password using the sub function. The modified content is then printed and redirected to a temporary file named temp.txt.

Afterward, the original password.txt file is replaced with the contents of temp.txt, effectively updating the password.

First, look at the content of the password file.

cat password.txt

String need to replace using awk commandThese are the passwords stored in the password.txt file. Now, run the awkreplace.sh script to replace the current Email password and store a new one.Finding and replacing text using awk command in bashOnce the program is executed it replaces the Email password with the newly inputted one which is 1445djshsy in this case.

Method 4: Find and Replace Text in File with the ‘perl’ Command

The perl command in Bash is old but extremely powerful. One can find and replace text in a single line of code using the perl command. The basic syntax of the command is given below.

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

You can follow the Steps of Method 2 to learn about creating and saving shell scripts.

Script (perlreplace.sh) >

#!/bin/bash
perl -pi -e 's/hidden/protected/g' find_replace.txt
EXPLANATION

This Bash script utilizes the perl command to perform an in-place search and replace operation on the contents of the file named find_replace.txt. The script employs a regular expression-based substitution using the -pi flag, which stands for in-place and loops over files. It replaces all occurrences of the word “hidden” with “protected” in the find_replace.txt file.

First, let’s look at the find_replace.txt file.

Cat find_replace.txt

1 File before replacement using perl command in bashAs you can see the content of the find_replace.txt file has the word “hidden” multiple times.

Now, run the perlreplace.sh script by the following command:

./prelreplace.sh

Visualizing text after replacement using perl command in bashOnce executed the program replaces the word “hidden” in all occurrences with the word “protected”.

NOTE: This process will recursively search all the occurrences and replace them with the new text. Remove ‘/g’ from the search string if you want to replace the first occurrence only. To find and replace in the first occurrence only use the perl -pi -e ‘s/old_text/new_text’ filename command.

Method 5: Find and Replace Across Multiple Files Using the ‘find’ and ‘sed’ Commands

Suppose you need to replace certain text from multiple files. In such a case, you can utilize the find command with exec to replace text from a couple of files at a time.

You can follow the Steps of Method 2 to learn about creating and saving shell scripts.

Script (findinfiles.sh) >

#!/bin/bash

for i in {1..10}; do
touch "dspace$i"
echo "It is a bash script file" > "dspace$i"
done

find . -type f -name "dspace*" -exec sed -i 's/It is bash script file/It is a text file with diskspace information/g' {} \;
EXPLANATION

The provided Bash script creates ten files named “dspace1” through “dspace10” using a loop. It then adds the text “It is a bash script file” into each of these files. After that, the script uses the find command to locate all files in the current directory with names starting with “dspace“.

For each of these files, the sed command is executed with the -i flag for in-place editing.

This command replaces the text “It is bash script file” with “It is a text file with diskspace information” in each of the located files, effectively modifying their contents.

Run the findinfiles.sh script by the following command:

./findindfiles.sh

Find and replace across multiple files using find command in bash

Once you execute the code it will create a total of 10 files from dspace1 to dspace10. Then it echoed the message “It is a bash script file” and redirected it to each of the created files. However, it’s an incorrect message so needs to be replaced by “It is a text file with diskspace information

Comparative Analysis of Methods

Here is a comparative analysis between the above-discussed methods. Take a look at the analysis to determine which method suits your need.

Method Advantage Disadvantage
nano editor
  • Can open and see the content.
  • Not from the command line.
sed command
  • Directly from the terminal.
  • Can’t visualize the content of the file.
awk command
  • Can replace all the matching occurrences.
  • Can’t do an in-place replacement.
perl command
  • Can replace all the matching occurrences in-place.
  • Old and limited functionality.
find command
  • Can replace matching text in multiple files at a time.
  • Must have a clear understanding to avoid syntax errors.

The best method for finding and replacing strings in Bash depends upon the nature of the task. For example, to replace text in multiple files, the find command is super useful. Moreover, one finds it easy to do tasks from the command line and some prefer an actual editor. So my suggestion is to choose the method that suits your needs.

Conclusion

To conclude, you can find and replace certain things in a file in various ways. The methods of this article cover almost all the default choices to find and replace text in Bash. Hope you enjoy reading this article.

People Also Ask

How do I replace a character in Bash?
To replace a character use a regular expression to match the single character. After that use any of the ways of replacing text.

How to find and replace in a shell script?
Shell Scripts are nothing but a file with the ‘.sh’ extension. You can find and replace text or string of a Bash script using any of the ways discussed in this article.

How to change the value of a variable by find and replace operation?
To change the value of a variable in any file use a regular expression to match the field and then replace the old value by new one.

Related Articles


<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment