Table of Contents
1. Overview
In this article, we will explore different ways to replace characters in String in different scenarios such as replacing the first or all occurrences of a given character, replacing all occurrences of multiple characters, and substituting a complete word.
2. Using Parameter Expansion
Bash’s built-in parameter expansion can be used to manipulate String in efficient way. We can use parameter expansion in different ways in various situations.
Let’s explore them below.
2.1 Replacing the First Occurrence
1 2 3 4 |
string="Words World" echo ${string/r/_} |
1 2 3 |
Wo_ds World |
This replaces first occurrence of r
with _
.
${string/r/_}
: The syntax follows this pattern: {variable//pattern/replacement}
.
In this specific case:
//r/_
tells Bash to replace first occurrence of r
with _
in the value of string.
To deep dive further:
/
indicates a single replacement (i.e., replace first occurrence, not just all).r
is the character we are looking to replace._
is the character we are replacing r with.
2.2 Replacing All Occurrences of a Character
1 2 3 4 |
string="Words World" echo ${string//r/_} |
1 2 3 |
Wo_ds Wo_ld |
This replaces all occurrences of r
with _
.
It is similar to previous one, except that //
indicates global replacement. This means it will replace all occurrences of the character rather than just one.
2.3 Replacing All Occurrences of Multiple Characters
1 2 3 4 |
string="AxxBCyyyDEFzzLMN" echo ${string//[xyz]/_} |
1 2 3 |
A__BC___DEF__LMN |
2.4 Replacing a Complete Word
1 2 3 4 |
string="Words World" echo ${string/Words/Hello} |
1 2 3 |
Hello World |
3. Using sed Command
The sed (Stream Editor) is a powerful and versatile text processing tool for text transformation. Let’s explore how can we use sed
command in different scenarios as covered in previous section.
3.1 Replacing the First Occurrence
1 2 3 4 |
string="Words World" echo $string | sed 's/r/_/' |
1 2 3 |
Wo_ds World |
's/r/_/'
: The syntax follows this pattern: {command//pattern/replacement}
.
In this specific case:
's/r/_/'
tells Bash to replace all occurrences of r
with _
in the value of string.
To deep dive further:
s
indicates a substitution command.r
is the character we are looking to replace._
is the character we are replacing r with.
3.2 Replacing All Occurrences of a Character
1 2 3 4 |
string="Words World" echo $string | sed 's/r/_/g' |
1 2 3 |
Wo_ds Wo_ld |
If we add g
at the end, it will replace all occurrences of r
with _
. g
is the flag used for global substitution.
3.3 Replacing All Occurrences of Multiple Characters
1 2 3 4 |
string="AxxBCyyyDEFzzLMN" echo $string | sed 's/[xyz]/_/g' |
1 2 3 |
A__BC___DEF__LMN |
3.4 Replacing a Complete Word
1 2 3 4 |
string="Words World" echo $string | sed 's/Words/Hello/' |
1 2 3 |
Hello World |
4. Using awk
Command
The awk is a powerful scripting language for text processing. Let’s use awk to replace characters in different scenarios.
Let’s learn each of them below.
4.1 Replacing the First Occurrence
1 2 3 4 |
string="Words World" echo $string | awk '{sub(/r/,"_"); print}' |
1 2 3 |
Wo_ds World |
Let’s understand more about awk
command:
sub(/r/,"_")
: The gsub function in awk is used for substitution. It replaces first occurrence of the pattern specified in the first argument (r in this case) with the replacement specified in the second argument (_).
print
: Print command prints modified String.
4.2 Replacing All Occurrences of a Character
1 2 3 4 |
string="Words World" echo $string | awk '{gsub(/r/,"_"); print}' |
1 2 3 |
Wo_ds Wo_ld |
This is similar to previous section except that we use gsub()
function for global replacement.
4.3 Replacing All Occurrences of Multiple Characters
1 2 3 4 |
string="AxxBCyyyDEFzzLMN" echo $string | awk '{gsub(/[xyz]/,"_"); print}' |
1 2 3 |
A__BC___DEF__LMN |
4.4 Replacing a Complete Word
1 2 3 4 |
string="Words World" echo $string | awk '{gsub(/Words/,"Hello"); print}' |
1 2 3 |
Hello World |
5. Using tr
Command
The tr
command is different from the last two approaches; it neither replaces the first occurrence of a character in a given string nor replaces an entire word with another word. This is because tr
works on a character-by-character basis. So, for example, we can replace the following using the tr
command:
- All occurrences of a character or multiple characters in a string.
Let’s explore it below.
5.1 Replacing All Occurrences of a Character
1 2 3 4 |
string="Words World" echo $string | tr 'r' '_' |
1 2 3 |
Wo_ds Wo_ld |
Here, tr 'r' '_'
is used to replace r
with _
.
5.2 Replacing All Occurrences of Multiple Characters
1 2 3 4 |
string="AxxBCyyyDEFzzLMN" echo $string | tr '[xyz]' '_' |
1 2 3 |
A__BC___DEF__LMN |
6. Conclusion
In this article, we used different ways to replace character in String in different replacement scenarios including replacing the first or all occurrences of a given character, replacing all occurrences of multiple characters, and substituting a complete word.
Generally, parameter expansion is quick and starightforward way for string replacements.