dark mode light mode Search
Search

How to Replace String in Bash [6 Best Ways]

TL;DR

Here are the six ways to replace string in Bash:

  1. Use the sed command to replace a string in Bash by executing sed 's/original/replacement/g' input-file > output-file.
  2. Run the awk command to replace a string in Bash by executing awk '{gsub(/original/, "replacement")}1' input-file > output-file.
  3. Try the tr command to replace a string in Bash by executing tr 'original' 'replacement' < input-file > output-file.
  4. Execute the Bash built-in functions for string manipulation, such as ${myString/brown/red} to replace the first occurrence of “brown” with “red”.
  5. Use regular expressions to replace strings in Bash by executing sed 's/[0-9]/X/g' input-file > output-file or awk '{gsub(/[0-9]/, "X")}1' input-file > output-file.
  6. Create Bash scripts to automate string replacement tasks by creating a script file with the code and executing it with ./replace_string.sh input-file search-string replacement-string output-file.

To replace strings in Bash efficiently, use built-in functions for simple tasks and sed or awk for larger files. Test commands on sample data and keep scripts modular. Use -n with sed and p commands to print specific lines in large files.

Bash is a popular Unix shell that offers powerful tools for string manipulation, such as modifying, extracting, or replacing parts of a string in a text file. This is useful when Linux programmers need to find and replace string in Bash in a variety of scenarios, such as data processing, system administration, log analysis, automation tasks, and more. So, in this comprehensive guide, you’ll get to know various techniques to replace strings in Bash that may come in handy to you as a Linux programmer.

How to Replace String in Bash [6 Easy Ways]

To replace string in Bash, you can use sed, awk, tr, and regex methods. Additionally, built-in string manipulation functions and Bash scripting can be used for automated string replacement. Here, I’ll provide step-by-step instructions for each of these methods:

1. sed Command

sed (stream editor) is a powerful text processing tool in Unix-based systems, primarily used for text substitution. Here’s how to use it:

  1. Launch the Terminal app and execute the command below to read the text file:
cat filename
  1. Now, run the command below to replace string in Bash.
sed 's/original/replacement/g' input-file > output-file
  1. This command will replace all occurrences of “original” with “replacement”. Now, you’ll see the following output using the cat command:
sed command to replace string in bash

2. awk Command

awk is a versatile text processing tool that operates with patterns and actions. You can use this command to replace string in Bash in the following way:

  1. Execute the command below to read the text file in the Terminal window:
cat filename
  1. Run the following command in the Linux command prompt:
awk '{gsub(/original/, "replacement")}1' input-file > output-file
  1. Now, you’ll see all occurrences of “string 1” replaced with “string 2”:
awk command to replace string in bash

3. tr Command

tr (translate) is a command for character-level translations and deletions. Follow these steps to use it to replace string in Bash:

  1. Run the cat command in the command prompt to read the file.
cat filename
  1. Enter the following command in the Terminal window:
tr 'original' 'replacement' < input-file > output-file
  1. tr 'aeiou' 'AEIOU' < text.txt > text_modified.txt will find and replace all lowercase vowels with their uppercase counterparts. Here is the output:
tr command to replace string in bash

4. Bash Built-In Functions

Bash provides built-in string manipulation functions using parameter expansion, substring replacement, and case modification. To replace string in Bash, try these built-in functions:

  1. Save the string “The quick brown fox jumps over the lazy dog” in a variable named “myString” with the following command:
myString="The quick brown fox jumps over the lazy dog"
  1. Now, you can use the variable “myString” in other commands to manipulate or display its contents. To confirm the variable value, run the echo $myString command in the Linux command prompt:
mystring command to manipulate or display contents in bash
  1. To replace only the first occurrence of a substring “brown” with “red” in the string, you can use the following command:
echo ${myString/brown/red}
  1. The output would be:
replace only first occurrence of a substring in bash
  1. You can also run the following command to replace all occurrences of a substring:
echo ${myString//e/a}
  1. If done correctly, it’ll replace string in Bash, and you’ll get the output in Terminal:
replace all occurrences of a substring in bash
  1. You can also convert a string to uppercase with the following command:
echo ${myString^^}
  1. You’ll get the following output in the Terminal window.
convert a string to uppercase in bash
  1. Similarly, if you want to replace string in Bash with its lowercase, run the following command:
echo ${myString,,}
  1. The output of this command would be:
convert a string to lowercase in bash

5. Regular Expressions

Regular expressions (regex) are a powerful way to define search patterns in text. It is a sequence of characters that specifies a search pattern. It can be used to match, find, or replace specific strings within a text. Here’s how you can use it:

  1. Create and save the text file with the following content:
123456789
The quick brown fox jumps over the 9 lazy dogs.
  1. Launch the Terminal app and execute the cat command to confirm the text file content.
match find replace specific strings within text in bash
  1. Now, use the sed command below to replace all digits with an ‘X’:
sed 's/[0-9]/X/g' input-file > output-file
  1. Use the cat command to see the output in your Terminal window.
replace specific strings within text in bash
  1. Alternatively, you can use the awk command to replace all digits with an ‘X’:
awk '{gsub(/[0-9]/, "X")}1' input-file > output-file
  1. You’ll see the following output using the cat command:
awk command to replace all digits within a text in bash

6. Bash Scripting and Automation

You can create reusable scripts and automate the string replacement tasks to replace string in Bash. This can save time and reduce errors. To do so, follow these steps:

  1. Create a Bash script file:
touch replace_string.sh
  1. Execute the nano command with the filename.
nano replace_string.sh
  1. Add the following content to the script:
#!/bin/bash
input_file="$1"
search_string="$2"
replacement_string="$3" 
output_file="$4"
sed "s/$search_string/$replacement_string/g" "$input_file" > "$output_file"  

This script takes four arguments: the input file name, the search string, the replacement string, and the output file name. It uses the sed command to replace all occurrences of the search string with the replacement string in the input file and saves the output to the specified output file.

  1. Press Ctrl + O and Enter to save. Then, press Ctrl + X to exit the Nano text editor.
scripting and automation in bash
  1. Make the script executable:
chmod +x replace_string.sh
  1. Execute the cat command to read the input file.
cat filename
  1. Run the script in the Terminal window:
./replace_string.sh input-file search-string replacement-string output-file
  1. Run the cat command to see the output:
execute scripting and automation in bash

4 Quick Tips to Replace String in Bash Efficiently

To replace string in Bash efficiently, use optimized techniques to do it. This is especially important when handling large datasets and complex automation processes. Consider the following four tips to optimize your string replacement techniques and improve your Bash scripting skills:

  • 🚀 Speed considerations: For simple string replacement tasks, use built-in Bash functions, as they are generally faster than external tools like sed and awk. However, you may still have to use sed or awk over Bash built-in functions for large files, as they handle the memory more efficiently.
  • 🧪 Test the string replacement: Always test your string replacement commands and scripts on sample data before applying them to critical files or production environments. Keep your scripts modular and reusable to reduce the likelihood of errors and improve maintainability.
  • 💾 Memory usage: Be mindful of memory usage when working with large files or data sets. sed and awk can be more memory-efficient than built-in Bash functions for such tasks.
  • 📄 Print specific lines: Use the -n option with sed to disable automatic printing of output, and then use the p command to print only specific lines that match your search pattern. This can save time and resources when working with large files.

In Conclusion

By now, you should have a solid understanding of various techniques to replace string in Bash using the sed, awk, tr, and regex methods. You can further explore Bash string manipulation to help you automate tasks and streamline your workflow.

However, your learning journey in Bash scripting doesn’t end here. There are many other aspects to explore, such as improving code readability with echo and using set x for debugging. You can also look into multiple ways to clear Bash history to experience efficient Bash performance. Continuously learning and growing will lead to success in the world of Unix shell scripting.

Frequently Asked Questions

How do single and double quotes differ in Bash string manipulation?

Single quotes in Bash preserve the literal value of all characters within the quotes, meaning that variables and special characters are not interpreted or expanded. Double quotes, on the other hand, allow for variable expansion and interpretation of certain special characters.

How can I undo a string replacement operation in Bash?

It is recommended to make a backup copy of the original file before performing any string replacement operations. If a mistake is made, you can restore the original file from the backup. Alternatively, some tools like sed have an undo option (e.g., -i.bak), which creates a backup file during string replacement that can be used to undo the operation.

Is it possible to perform a case-insensitive string replacement in Bash?

Yes, for case-insensitive string replacements, use sed with the I flag within the s command, like sed 's/search/replacement/gI'. Alternatively, use awk with tolower() and toupper() functions to convert strings to the same case before comparison.

Can I perform string replacement on multiple lines or a block of text using Bash, sed, or awk?

Yes, string replacement on multiple lines or a block of text can be done in sed and awk by using regex patterns and options that match multiple lines or blocks of text. In Bash, use the tr or perl command with the s option to perform the replacement on multiple lines or a block of text.

How can I replace strings in multiple files at once using Bash, sed, or awk?

To replace strings in multiple files, use the find command to locate the files and the xargs command to pass them as input to sed or awk. For example, to replace “foo “with “bar” in all files with a .txt extension, use find . -name "*.txt" -print0 | xargs -0 sed -i 's/foo/bar/g'.

Total
0
Shares