TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: How to Use Bash to Replace Character in String
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
HowToLinux Tutorials

How to Use Bash to Replace Character in String

Last updated: Apr 23, 2024 2:42 pm
By Meenakshi Agarwal
Share
14 Min Read
Bash Methods to Replace Character in String
SHARE

If you’re working with Bash, understanding how to replace characters in a string is a handy skill. Let’s dive into this topic in simple terms with various methods and plenty of examples.

Contents
1. Use Parameter ExpansionBasic SyntaxExample1: Simple Character ReplacementExample2: Replace All OccurrencesExample3: Replace SubstringExample4: Conditional ReplacementExample5: Replace Only at the BeginningExample6: Replace Only at the End2. Using Sed to Replace Character in BashBasic SyntaxExample1: Simple Character ReplacementExample2: Replace All OccurrencesExample3: Replace SubstringExample4: Using VariablesExample5: In-Place Editing of a FileExample6: Case-Insensitive Replacement3. Run Awk to Replace Character in a StringBasic SyntaxExample1: Simple Character ReplacementExample2: Replace SubstringExample3: Using VariablesExample4: Customizing Output FormatExample5: Replace Only in Specific Fields4. Make Use of tr CommandBasic SyntaxExample 1: Simple Character ReplacementExample2: Replace Multiple CharactersExample3: Replace with SpaceExample4: Translate Sets of CharactersExample5: Delete CharactersExample6: Replace Spaces with Underscores5. Using Substring in Bash to Replace CharacterBasic SyntaxExample1: Simple Substring ReplacementExample2: Replace Substring GloballyExample3: Replace at the BeginningExample4: Replace at the EndExample5: Dynamic Substring ReplacementExample6: Replace Only if Another Substring is Present6. Nested Parameter Expansion in Bash to Replace CharacterExample

Multiple Ways to Replace Characters in String Using Bash

Learning bash scripting is an amazing skill for those working on Linux. As a Linux user, you may face a challenge like using bash to replace characters in a string. For this, there are multiple solutions that we brought into this tutorial. Check each of these methods one after the other.

1. Use Parameter Expansion

Bash Parameter Expansion is a powerful feature that allows you to manipulate variables, and it includes a syntax for replacing characters within a string. The general pattern for character replacement using parameter expansion is:

Basic Syntax

${var/pattern/replacement}

Here’s a breakdown of each component:

  • ${var}: This represents the variable containing the main string.
  • /: This is the delimiter that separates the variable, pattern, and replacement.
  • pattern: This is the substring or character you want to replace.
  • replacement: This is the new substring or character that will replace the old one.

Let’s explore this with a few examples:

Example1: Simple Character Replacement

orig="hello"
mod="${orig/o/O}"
echo $mod  # Output: hellO

In this example, we’re replacing the first occurrence of ‘o’ with ‘O’ in the variable orig.

Example2: Replace All Occurrences

orig="banana"
mod="${orig//a/X}"
echo $mod  # Output: bXnXnX

Here, we’re replacing all occurrences of ‘a’ with ‘X’ in the variable orig.

Example3: Replace Substring

orig="abcdefg"
mod="${orig/abc/XYZ}"
echo $mod  # Output: XYZdefg

This example demonstrates replacing the substring 'abc' with ‘XYZ’ in the variable orig.

Example4: Conditional Replacement

orig="apple"
mod="${orig/a/}"  # Replace first 'a' with nothing
echo $mod  # Output: pple

In this case, we’re replacing only the first occurrence of ‘a’ with an empty string, effectively removing it.

Example5: Replace Only at the Beginning

orig="apple"
mod="${orig/#a/X}"  # Replace 'a' only if it's at the beginning
echo $mod  # Output: Xpple

Using /# ensures that the replacement only occurs if ‘a’ is at the beginning of the string.

Example6: Replace Only at the End

orig="apple"
mod="${orig/%e/X}"  # Replace 'e' only if it's at the end
echo $mod  # Output: applX

With /%, the replacement happens only if ‘e’ is at the end of the string.

Bash Parameter Expansion provides a concise and convenient way to perform character replacements within strings, offering various options for different scenarios. It’s a valuable tool in your Bash scripting toolkit.

2. Using Sed to Replace Character in Bash

Sed, short for stream editor, is a powerful command-line tool for text processing and manipulation. It’s particularly useful for replacing characters or patterns in strings within files or streams. Let’s explore how to use Sed to replace characters in a string:

Basic Syntax

echo "orig_str" | sed 's/pattern/replacement/'

Here’s a breakdown of the components:

  • orig_str: The input string you want to modify.
  • pattern: The substring or regular expression you want to replace.
  • replacement: The new substring that will replace the matched pattern.

Example1: Simple Character Replacement

orig="hello"
mod=$(echo $orig | sed 's/o/O/')
echo $mod  # Output: hellO

In this example, we’re using sed to replace the first occurrence of ‘o’ with ‘O’ in the variable orig.

Example2: Replace All Occurrences

orig="banana"
mod=$(echo $orig | sed 's/a/X/g')
echo $mod  # Output: bXnXnX

Here, we’re replacing all occurrences of ‘a’ with ‘X’ in the variable orig using the global (g) flag.

Example3: Replace Substring

line="This is a sample line."
mod=$(echo $line | sed 's/is/at/')
echo $mod  # Output: That is a sample line.

This example demonstrates replacing the first occurrence of ‘is’ with ‘at’ in the variable line.

Example4: Using Variables

pattern='apple'
rep='orange'
orig="I have an apple."
mod=$(echo $orig | sed "s/$pattern/$rep/")
echo $mod  # Output: I have an orange.

You can use variables for the pattern and replacement to make your sed command more dynamic.

Example5: In-Place Editing of a File

# Replace 'old' with 'new' in a file.txt (in-place)
sed -i 's/old/new/' file.txt

The -i flag allows in-place editing of the file. Be cautious, as this modifies the file directly.

Example6: Case-Insensitive Replacement

orig="Hello World"
mod=$(echo $orig | sed 's/hello/hi/i')
echo $mod  # Output: hi World

Using the i flag makes the replacement case insensitive.

Sed provides a flexible and efficient way to replace characters or patterns in strings. It’s especially handy for automating text transformations in scripts and one-liners. Remember to adapt the command based on your specific use case and requirements.

3. Run Awk to Replace Character in a String

Awk is a powerful programming language for text processing, and it can be used to replace characters or patterns in strings within a Bash script. Let’s explore how to use Awk for character replacement:

Basic Syntax

echo "orig_string" | awk '{gsub(/pattern/, "replacement"); print}'

Here’s a breakdown of the components:

  • orig_string: The input string you want to modify.
  • /pattern/: The regular expression or pattern you want to replace.
  • "replacement": The new string that will replace the matched pattern.
  • {gsub(...)}: The Awk function for global substitution.
  • print: Outputs the mod string.

Example1: Simple Character Replacement

orig="hello"
mod=$(echo $orig | awk '{gsub(/o/, "O"); print}')
echo $mod  # Output: hellO

In this example, we’re using Awk to replace all occurrences of ‘o’ with ‘O’ in the variable orig.

Example2: Replace Substring

st="This is a sample word."
mod=$(echo $st | awk '{gsub(/is/, "at"); print}')
echo $mod  # Output: That at a sample word.

Here, we’re replacing the first occurrence of ‘is’ with ‘at’ in the variable st.

Example3: Using Variables

pat='apple'
rep='orange'
orig="I have an apple."
mod=$(echo $orig | awk -v pat="$pat" -v rep="$rep" '{gsub(pat, rep); print}')
echo $mod  # Output: I have an orange.

You can use the -v option to pass variables into Awk for dynamic replacements.

Example4: Customizing Output Format

orig="123-456-789"
mod=$(echo $orig | awk '{gsub(/-/, " "); print}')
echo $mod  # Output: 123 456 789

In this case, we’re replacing hyphens with spaces and customizing the output format.

Example5: Replace Only in Specific Fields

data="John,Doe,30"
mod=$(echo $data | awk 'BEGIN{FS=OFS=","} {gsub(/Doe/, "Smith", $2); print}')
echo $mod  # Output: John,Smith,30

Awk allows you to specify the field separator (FS) and output field separator (OFS) and target replacements within specific fields.

Awk provides a flexible and expressive way to replace characters or patterns in strings. It’s particularly useful when dealing with structured text data. Customize your Awk commands based on the complexity and requirements of your specific use case.

4. Make Use of tr Command

The tr command in Bash is a simple yet effective tool for translating or deleting characters. It’s commonly used for basic character replacements in strings. Let’s explore how to use the tr command for character replacement:

Basic Syntax

echo "orig_str" | tr 'old_char' 'new_char'

Here’s a breakdown of the components:

  • orig_str: The input string you want to modify.
  • 'old_char': The character you want to replace.
  • 'new_char': The character that will replace the old one.

Example 1: Simple Character Replacement

orig="hello"
mod=$(echo $orig | tr 'o' 'O')
echo $mod  # Output: hellO

In this example, we’re using tr to replace all occurrences of ‘o’ with ‘O’ in the variable orig.

Example2: Replace Multiple Characters

orig="abc123"
mod=$(echo $orig | tr 'a-c1-3' 'X')
echo $mod  # Output: XXXXXX

Here, we’re replacing characters ‘a’ through ‘c’ and digits ‘1’ through ‘3’ with ‘X’.

Example3: Replace with Space

st="Hello-World"
mod=$(echo $st | tr '-' ' ')
echo $mod  # Output: Hello World

This example replaces hyphens with spaces in the variable sentence.

Example4: Translate Sets of Characters

text="apple"
mod=$(echo $text | tr 'a-z' 'A-Z')
echo $mod  # Output: APPLE

Using character ranges, we’re translating lowercase letters to uppercase in the variable text.

Example5: Delete Characters

orig="remove-vowels"
mod=$(echo $orig | tr -d 'aeiou')
echo $mod  # Output: rmv-vwls

The -d option deletes specified characters (vowels in this case) from the variable orig.

Example6: Replace Spaces with Underscores

phrase="Hello World"
mod=$(echo $phrase | tr ' ' '_')
echo $mod  # Output: Hello_World

Here, we’re replacing spaces with underscores in the variable phrase.

The tr command is efficient for simple character replacements and is suitable for scenarios where you need to perform basic transformations on a string. Customize your tr command based on your specific replacement needs and the characters involved.

5. Using Substring in Bash to Replace Character

In Bash, you can replace characters within a substring using parameter expansion. This method allows you to target a specific portion of the string rather than replacing characters globally. Let’s explore how to use substring replacement in Bash:

Basic Syntax

${variable/pattern/replacement}

Here, variable is your orig string, pattern is the substring or character you want to replace, and replacement is the new substring or character that will replace the old one.

Example1: Simple Substring Replacement

orig="hello world"
mod="${orig/lo/LA}"
echo $mod  # Output: helLA world

In this example, we’re replacing the first occurrence of the substring ‘lo’ with ‘LA’ in the variable orig.

Example2: Replace Substring Globally

orig="apple orange apple"
mod="${orig//apple/APPLE}"
echo $mod  # Output: APPLE orange APPLE

Here, we’re replacing all occurrences of the substring ‘apple’ with ‘APPLE’ in the variable orig.

Example3: Replace at the Beginning

filename="file.txt"
mod="${filename/#file/Document}"
echo $mod  # Output: Document.txt

Using /# ensures that the replacement only occurs if ‘file’ is at the beginning of the string in the variable filename.

Example4: Replace at the End

filename="image.jpg"
mod="${filename/%jpg/png}"
echo $mod  # Output: image.png

With /%, the replacement happens only if ‘jpg’ is at the end of the string in the variable filename.

Example5: Dynamic Substring Replacement

substring='abc'
replacement='XYZ'
orig="abcdefg"
mod="${orig/$substring/$replacement}"
echo $mod  # Output: XYZdefg

You can use variables for the substring and replacement to make your substring replacement dynamic.

Example6: Replace Only if Another Substring is Present

orig="abcdefg"
substring='abc'
replacement='XYZ'
mod="${orig/$substring/${orig/*def*/$replacement}}"
echo $mod  # Output: XYZg

Here, the replacement occurs only if ‘def’ is present in the variable orig after the substring 'abc'.

Substring replacement in Bash provides a precise way to modify specific parts of a string. Customize the command based on your use case, ensuring that the pattern you’re matching accurately reflects the substring you want to replace.

6. Nested Parameter Expansion in Bash to Replace Character

As of my last knowledge update in January 2022, Bash does not support true nested parameter expansion. However, you can achieve a similar effect by using multiple parameter expansions sequentially. Let me demonstrate a method that simulates nested behavior for character replacement in a string.

Example

orig="abcdefg"
outer='abc'
inner='def'
replacement='XYZ'

# Perform outer replacement
mod="${orig/$outer/}"

# Perform inner replacement (within the result of outer replacement)
mod="${mod/$inner/$replacement}"

echo $mod  # Output: XYZg

In this example, we first perform an outer replacement to remove the substring specified by outer. Then, within the result of the outer replacement, we perform an inner replacement to replace the inner with the desired replacement. The final result is XYZg.

This method allows you to achieve a sequential replacement effect, simulating the concept of nested parameter expansion.

It’s essential to be cautious when using this approach, as the order of replacement matters. Ensure that the outer replacement doesn’t inadvertently affect the subsequent replacements.

In a nutshell, mastering character replacements in Bash is crucial for effective scripting. Choose the method that suits your task complexity, and stay updated on the latest Bash features for enhanced functionalities. Happy scripting!

You Might Also Like

How to Fix Accessibility Issues With Tables in WordPress

How to Use Python To Generate Test Cases for Java Classes

Basic Linux Commands for Beginners With Examples

Sorting List of Lists in Python Explained With Examples

How to Fetch the List of Popular GitHub Repos

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article 7 Ways to Remove Whitespace from a String in Python How to Remove Whitespace from a String in Python
Next Article Cypress vs Selenium Comparison and Differences Cypress vs Selenium – Which One Should You be Using in 2024

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x