Home Learn Linux Replacing String in Bash

Replacing String in Bash

by Abraham
Published: Last Updated on
replacing string in Bash

Bash is a UNIX shell-compatible command process whose main task is to manipulate strings conducted in a shell environment. Programmers are at times called upon to work on different files. They can add, delete, and replace parts or the whole file to fit their work. This calls upon the knowledge of replacing string in bash. Data storage can be temporary or permanent, depending on the nature of the data. File string is essential when replacing file contents.

Replacing String in Bash

sed is vital and comes in handy to aid in replacing strings in a file with bash scripts. The string editor can be used in different ways to replace file contents in bash. An alternate but sound string editor is awk which can be brought into play when replacing string value from a file using bash. This article will show with the aid of examples how the file content can be replaced in bash. Note that knowledge in regex patterns can also be of great value when replacing string in bash.

Bash String Manipulation

When doing minor or simple replacements, the sed is not required. Bash can do the manipulation.

For example:
When replacing a fosslinux.gif with .mp4, sed is not required.
To execute the process using bash, use the coded text below:

#/bin/bash
file_name="fosslinux.gif"
new_extension="mp4"

# Replace .gif with .mp4
generated_file_name=${file_name/.gif/.$new_extension}

echo $generated_file_name

Replacing String using sed

sed can be referred to as a stream editor. Sed can replace a string in files. Let’s look at an example of a file called singleboardbytes.txt that has the following information:
Foss Linux is a great learning site for any Linux user. Check out articles published by Foss Linux on their site.

Now from our file, we need to replace the word “site” with “website” and rename the file to fosslinux.txt.

Understanding the sed syntax

sed -i 's/search_string/replace_string/' filename
sed – is the command used in the execution of the above syntax
‘-i’ – used in the modification of contents in the original file with a replacement string aid whenever the search value exists in the file.
‘s’ – this is the substitute command
‘search_string’ – this represents the string value that is to be searched for replacement in the original file
‘replace_string’ – this represents the string value that is used to replace the searched value. The string should match the string found by the ‘search_string’ command.
‘filename’ – this is the filename whereby the replacement will be applied.

Execute the following command in your terminal:

sed 's/site/website/g' singleboardbytes.txt > fosslinux.txt
String Replacement using sed

String Replacement using sed

Note: you can also execute the command above by putting it on as a bash script.
Replacing using the -i command

After running the command, the next step is to replace using the -i command. -i is a representation for “in place”, meaning the file is only replaced, and there is no creation of extra files. Using our example above, run the following command:

sed -i 's/site/website/g' singleboardbytes.txt
Replacing using the -i command

Replacing using the -i command

From the figure below, you can see an exclamation mark that says, The file “/home/tuts/singleboardbytes.txt changed on disk.” this shows that the changes have been successfully made.

Changes made to singleboardbytes.txt

Changes made to singleboardbytes.txt

The figure below is the file containing the changes made in the previous step, both to the name of the file (singleboardbytes.txt to fosslinux.txt) and the word site has been changed to the website.

Final change after running the sed command

Final change after running the sed command

Using a variable as search values and replacements

With a little interpolation, variables can be used to do string replacement. The first sed argument, in this case, has to use double quotes instead of single quotes. For instance, your code will be similar to the one below:

tuts() {
local search=$1
local replace=$2

# Note the double quotes
sed -i "s/${search}/${replace}/g" singleboardbytes.txt
}
Using a variable as search values and replacements

Using a variable as search values and replacements

sed has two flavors, namely: the FreeBSD and the Linux sed. The Linux distros use the Linux sed while OS X users use the FreeBSD sed. These flavors are quite different, and hence one cannot transfer commands from one sed flavor to another.

For instance, while replacing the file’s contents using the FreeBSD, a backup should be conducted using the .bak extension. Or you can initiate a backup with the use of empty pairs of quotes immediately after the -i notation as shown in the bash syntax below:

tuts() {
local search=$1
local replace=$2

sed -i "" "s/${search}/${replace}/g" singleboardbytes.txt
}

If a backup is not done while using the FreeBSD as illustrated in the syntax code above, you will fall into an error like the one shown below:

sed: 1: "/Users/user/path/tuts ...": extra characters at the end of h command

How to replace a file and match the digit pattern using the sed command

If you successfully run the script, a search for all numerical content present in a file will be performed. Then the content will then be replaced by adding $ before the numbers.

#!/bin/bash
# Check the command line argument value exists or not
if [ $1 != "" ]; then
# Search all string containing digits and add $
sed -i 's/\b[0-9]\{5\}\b/$&/g' $1
fi

Replacing a string using the awk command

In this case, we shall be using the awk command instead of the sed command to replace a file’s contents. The difference among these commands is that the sed command directly updates the original file, which is not the case with the awk command.

#!/bin/bash
# Check the command line argument value exists or not
if [ $1 != "" ]; then
# Search all string based on date
awk '{sub("02/06/2020","12/06/2020")}1' $1 > temp.txt && mv temp.txt $1
fi

Conclusion

The above examples are vital for your progress in learning how to replace a string in Bash. Therefore, to be good at it, you will have to practice regularly. Searching and replacing string in a file might seem complex, but you will find it much more manageable when following our guidelines correctly.

A point to note is that running the commands might at times seem more manageable, but you should be careful not to wreak havoc from sed. When you input the wrong commands, you will break your computer, causing you more trouble like data loss. Hence, to be safe, always copy and paste the code as it is to your terminal. This will prevent spelling errors that might break your machine. We hope this article helped you learn how to replace a string in bash using this article.

You may also like

2 comments

peter xie December 17, 2021 - 1:29 AM

echo generated_file_name
=> echo $generated_file_name

Reply
Waldo February 14, 2022 - 6:32 PM

Great article, it was very helpful in understanding the discrepancies in a bash script we were using on different operating systems.

Reply

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.