Replace one substring for another string in shell script
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In shell scripting, replacing a substring with another string is a common text manipulation task. This can be useful in a variety of scenarios such as configuring template files, cleaning up data, or customizing messages. In this article, we'll explore several ways to perform this operation using different shell utilities, including sed, awk, Bash itself, and perl. Each of these tools offers unique advantages depending on the complexity of the task and the environment in which you are working.
Using sed for Substring Replacement
The sed (stream editor) utility is very powerful for performing text transformations on a stream or in a file. It's particularly useful for simple replacements and works well with other Unix utilities in pipelines. The basic syntax to replace a substring is:
Here, s stands for substitution. The old-string is the text to be replaced, and new-string is the replacement text. If you want to replace all occurrences of the substring in each line, append the g flag at the end:
To change the file in-place, use the -i option:
Using awk for Substring Replacement
awk is a text-processing language that is useful for more complex transformations. It is particularly good for handling data that is structured into rows and columns. To replace a substring:
The gsub function (global substitution) replaces all occurrences of old-string with new-string. This action is performed on each line, and the result is printed.
Using Bash Built-in Feature
For simpler tasks where you are already using Bash, you can use its built-in string manipulation features. This is convenient and does not require calling external programs:
This uses Bash parameter expansion to replace all occurrences of old-string with new-string.
Using Perl for Substring Replacement
Perl is a powerful programming language with strong text processing abilities. For one-time commands from the shell, Perl can be very effective too:
This command modifies the file in place (-i), reading line by line (-p), and replaces every instance of old-string with new-string.
Summary Table
| Tool | Command Example | In-place Edit | Dependencies |
| sed | sed 's/old-string/new-string/g' filename | Yes, with -i | None (standard on Unix-like systems) |
| awk | awk '{gsub(/old-string/, "new-string")}' filename | No | None (standard on Unix-like systems) |
| Bash | ${str//old-string/new-string} | N/A | None (built into Bash) |
| Perl | perl -pi -e 's/old-string/new-string/g' filename | Yes | Perl needs to be installed |
Conclusion
Choosing the right tool for substring replacement depends on your specific needs, including the complexity of the task, performance considerations, and system environment. sed and Bash are typically preferred for simpler tasks, while awk and Perl offer more flexibility and power for complex data manipulations. Keep the above table handy for a quick reference to the syntax used by each tool for this common scripting requirement.
Additional Tips and Considerations
- Always backup files before performing in-place replacements.
- Test your command on a sample of the data before running it on the entire file.
- Be aware of special characters in
old-stringandnew-stringwhich might need to be escaped depending on the tool and context (e.g.,&,/,\). - Utilize regular expressions when you need to match patterns rather than fixed strings for more dynamic replacements.

