Replace Character in String in Bash

Bash replace character in String

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

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

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

2.4 Replacing a Complete Word

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

'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

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

3.4 Replacing a Complete Word

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

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

This is similar to previous section except that we use gsub() function for global replacement.

4.3 Replacing All Occurrences of Multiple Characters

4.4 Replacing a Complete Word

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

Here, tr 'r' '_' is used to replace r with _.

5.2 Replacing All Occurrences of Multiple Characters

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *