Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Bash Replace Character in String

Using Parameter Expansion

We can use parameter expansion in different ways in various situations. For example, we can use it to Replace the following:

  • The first or all occurrences of the given character in a string.
  • All occurrences of multiple characters in a string.
  • A complete word with another word in a string.

Let’s explore them below.

Use parameter expansion to replace the first occurrence of a character in the specified string in bash.

string="Words World"
echo ${string/r/_}
Wo_ds World

Use parameter expansion to replace all occurrences of a character in the specified string in bash.

string="Words World"
echo ${string//r/_}
Wo_ds Wo_ld

Use parameter expansion to replace all occurrences of multiple characters in the given string in bash.

string="AxxBCyyyDEFzzLMN"
echo ${string//[xyz]/_}
A__BC___DEF__LMN

Use parameter expansion to replace an entire word with another word in a string in bash.

string="Words World"
echo ${string/Words/Hello}
Hello World

For the first example, we declared a variable named string and initialized it with the Words World value. In the second line, we used parameter expansion to replace the r character’s first occurrence in the string variable with another character, _ (an underscore). The updated string was then passed to the echo command as an argument, which printed it on the Bash console. See the above output.

The second code example was the same as the previous one, but both r characters were replaced with _ this time due to using // preceded by r. In the third example, we used // again for global replacement, while all characters we wanted to replace were enclosed within []. The fourth and final example was the same as the first code snippet in this section. But now, we specify words rather than characters.

Using sed Command

We can use the sed command differently based on the situation, such as we can use this command to replace:

  • The first or all occurrences of the specified character in a string.
  • All occurrences of multiple characters in a string.
  • A complete word with another word in a string.

Let’s learn them below.

Use the sed to replace the first occurrence of a character in the specified string in Bash.

string="Words World"
echo $string | sed 's/r/_/'
Wo_ds World

Use the sed to replace all occurrences of a character in the specified string in Bash.

string="Words World"
echo $string | sed 's/r/_/g'
Wo_ds Wo_ld

Use the sed to replace all occurrences of multiple characters in the given string in Bash.

string="AxxBCyyyDEFzzLMN"
echo $string | sed 's/[xyz]/_/g'
A__BC___DEF__LMN

Use the sed to replace an entire word with another word in the mentioned string in Bash.

string="Words World"
echo $string | sed 's/Words/Hello/'
Hello World

In the first example, we defined the $string variable and set its value with Words World. Then, we used the sed command to replace an r character with _ in the $string variable. The final / is the mandatory delimiter; otherwise, we will get an error.

The second example is the same as the first, but we used the /g flag for a global replacement to replace all r characters with _. In the third, we wrapped all characters within square brackets ([]) to replace while /g was used to replace all occurrences of those characters. In the final example, we replaced a whole word with another word, such as we replaced Words with Hello.

For all the examples in this section, we used the sed command, the text stream editor, to edit files efficiently and quickly in Unix systems. It searches, adds, replaces, and removes lines in the specified text file without opening it in any text editor. We can also use it with string-type variables.

Using awk Command

We can use the awk command to replace:

  • The first occurrence or all occurrences of a character in a string.
  • All occurrences of multiple characters in a string.
  • A complete word with another word in a string.

Let’s learn each of them below.

Use the awk command to replace the first occurrence of a character in the specified string in Bash.

string="Words World"
echo $string | awk '{sub(/r/,"_"); print}'
Wo_ds World

Use the awk command to replace all occurrences of a character in the specified string in Bash.

string="Words World"
echo $string | awk '{gsub(/r/,"_"); print}'
Wo_ds Wo_ld

Use the awk command to replace all occurrences of multiple characters in the given string in Bash.

string="AxxBCyyyDEFzzLMN"
echo $string | awk '{gsub(/[xyz]/,"_"); print}'
A__BC___DEF__LMN

Use the awk command to replace an entire word with another word in the mentioned string in Bash.

string="Words World"
echo $string | awk '{gsub(/Words/,"Hello"); print}'
Hello World

Here, we used the awk command, which is used to write Linux/Unix shell scripts. It is used to process text-based data, whether retrieved from specific files, using shell pipes or data streams. In the first example, we performed a single substitution using the sub() method to replace only the first occurrence of the r character with _ in the $string variable. On the other hand, in the second, third, and fourth examples, the gsub() was used to replace globally.

The sub() and gsub() methods took two arguments, the first was the regex pattern that we wanted to match, and the second was the replacement. Finally, we used the print statement to display the modified $string on the Bash console.

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. 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.

Use the tr to replace all occurrences of a character in the specified string in Bash.

string="Words World"
echo $string | tr 'r' '_'
Wo_ds Wo_ld

Use the tr to replace all occurrences of multiple characters in the given string in Bash.

string="AxxBCyyyDEFzzLMN"
echo $string | tr '[xyz]' '_'
A__BC___DEF__LMN

That’s all about bash replace character in String.



This post first appeared on How To Learn Java Programming, please read the originial post: here

Share the post

Bash Replace Character in String

×

Subscribe to How To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×