Shell Scripting
Substring Replacement
Coding
Programming
Script Modification

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:

bash
sed 's/old-string/new-string/' filename

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:

bash
sed 's/old-string/new-string/g' filename

To change the file in-place, use the -i option:

bash
sed -i 's/old-string/new-string/g' filename

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:

bash
awk '{gsub(/old-string/, "new-string"); print}' filename

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:

bash
str="Hello old-string, meet new-string"
new_str="${str//old-string/new-string}"
echo "$new_str"

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:

bash
perl -pi -e 's/old-string/new-string/g' filename

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

ToolCommand ExampleIn-place EditDependencies
sedsed 's/old-string/new-string/g' filenameYes, with -iNone (standard on Unix-like systems)
awkawk '{gsub(/old-string/, "new-string")}' filenameNoNone (standard on Unix-like systems)
Bash${str//old-string/new-string}N/ANone (built into Bash)
Perlperl -pi -e 's/old-string/new-string/g' filenameYesPerl 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-string and new-string which 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.

Course illustration
Course illustration

All Rights Reserved.