1. Introduction

As Linux users, we’re probably familiar with the power and efficiency of sed for finding and replacing texts in files and streams. However, there are instances when certain Linux systems, especially those running on embedded devices or with limited resources, may not have the sed command readily available. In such cases, it becomes essential to explore other tools and techniques for accomplishing text substitutions.

In this tutorial, we’ll dive into various alternatives to sed that are just as effective, if not more, for handling find-and-replace tasks. First, we’ll discuss the key concepts of these versatile alternatives. Then, we’ll explore practical code examples and discover how to use them to streamline text processing on our Linux systems. Let’s get started!

2. The tr Command

Let’s start with the classic alternative to sed – the tr command. While tr might be known for its primary function of translating characters, it’s also handy for basic find-and-replace operations. One of the major advantages of tr is its availability on almost all Linux systems, making it a reliable choice for text manipulation tasks.

To better understand how tr works, let’s consider a simple example of the following text:

The quick brown fox jumps over the lazy dog.

Now, we want to replace all occurrences of the letter ‘o’ with ‘x’. Let’s achieve this using tr:

$ echo "The quick brown fox jumps over the lazy dog." | tr 'o' 'x'
Thx quick brxwn fxx jumps xver the lazy dxg.

As we can see, tr replaced all ‘o’ instances with ‘x’ in the text. The tr command operates per character and performs the substitution character by character.

Furthermore, the simplicity and speed of tr make it an ideal choice for single-letter substitutions. Compared to sed, a more powerful stream editor, using tr for such straightforward tasks can be akin to using a precision tool for a simple job.

3. Shell Parameter Expansion

In addition to the tr command, many shells, such as Ksh, Zsh, and Bash, offer built-in parameter expansion capabilities that allow us to manipulate variable values by replacing specific patterns with the desired text.

Let’s see an example using Bash. Suppose we have a variable greeting containing the text “Hello, world!”. Now, we want to replace all instances of “world” with “universe:

greeting="Hello, world!"
echo "${greeting//world/universe}"

In this example, the ${greeting//world/universe} expression performs a simple substitution, replacing all occurrences of “world” with “universe” in the greeting variable.

4. Powerful Text Processing With awk

Regarding versatile text processing, awk is a powerful pattern scanning and manipulation tool. While its primary purpose is not text substitution, awk is well-equipped to handle find-and-replace operations easily. Let’s explore the world of awk and see how to use it as a robust alternative to sed for text manipulation tasks.

4.1. Global Substitution Using gsub

The gsub function in awk performs global find-and-replace tasks within a given input string or file. It replaces all occurrences of a specified pattern with the desired replacement text. Let’s illustrate this with an example.

Suppose we have a file data.txt containing:

Hello, World! This is an example.
This is another example.

Now, let’s replace all occurrences of “example” with “illustration” in the file data.txt using the awk command and gsub function:

$ awk '{ gsub(/example([[:space:][:punct:]]+|$)/, "illustration&"); print }' data.txt > temp.txt && mv temp.txt data.txt

Here, we should note that using /example([[:space:][:punct:]]+|$)/ will match “example” followed by one or more spaces, tabs, or punctuation marks, or “example” occurring at the end of a line or sentence:

  • ‘{ gsub(/example([[:space:][:punct:]]+|$)/, “illustration&”); print }’ – performs the global substitution
  • data.txt – the input file on which we want to perform the global substitution
  • > temp.txt – redirects the output of the awk command to a temporary file temp.txt
  • && mv temp.txt data.txt – updates the original file (data.txt) with the changes by renaming the temporary file to data.txt

Upon running the command, the gsub function searches for the patternexample” in each line and replaces it with “illustration“. The modified content is then redirected to a temporary file and returned to the original one.

4.2. Handling Field-Specific Replacements

One of the remarkable features of awk is its ability to process data based on fields. This capability enables us to replace specific columns in a structured dataset.

Suppose we have a file employees.txt that contains:


Now, let’s say we want to replace “Johnson” with “Brown” only in the second column (last names). We can achieve this using the awk command:

$ awk 'BEGIN{FS=OFS=","} { if ($2 == "Johnson") $2 = "Brown"; print }' employees.txt > temp.txt && mv temp.txt employees.txt

In this example, we use awk to set the field separator (FS) and output field separator (OFS) to comma (,) using BEGIN{FS=OFS=”,”}. Then, we check if the second field ($2) is equal to “Johnson“, and if so, we replace it with “Brown“. The modified content is saved back to the original file.

Ultimately, awk is a versatile tool beyond simple find-and-replace operations. Its ability to handle complex text processing tasks makes it a compelling alternative to sed.

5. Versatility of Perl for Text Manipulation

When it comes to advanced regular expressions and intricate text manipulations, Perl emerges as a frontrunner. It excels in handling complex pattern matching and replacing, making it a strong alternative to sed for find-and-replace operations. Let’s explore how to use Perl effectively for text substitution tasks.

5.1. Find-and-Replace With Perl In-Place Editing

At its core, Perl utilizes regular expressions to perform powerful text replacements. Furthermore, Perl supports in-place editing, allowing us to modify files directly without creating temporary files.

Let’s start with an example of using Perl to replace all occurrences of “apple” with “kiwi” in a file data.txt using a Perl one-liner:

$ perl -i -pe 's{apple}{kiwi}g' data.txt

In this substitution expression:

  • -i performs in-place editing
  • -pe enables perl to read the input file line by line and apply the perl code specified with -e

Upon execution, the Perl one-liner performs the find-and-replace operation on the data.txt file, replacing all occurrences of “apple” with “kiwi” and saving the modified content to the original file.

5.2. Performing Conditional Replacements

One of the strengths of Perl lies in its ability to execute complex logic while performing substitutions. We can use conditions to determine whether a replacement should occur or not.

Let’s consider another example file, numbers.txt:


Now, we want to replace the standalone number “456” with “999” in the file numbers.txt, but only if the standalone number is greater than or equal to 400. Let’s achieve this with a Perl one-liner:

$ perl -i -pe 's/\b(456)\b/$1 >= 400 ? "999" : $1/ge' numbers.txt

Let’s better understand the conditional statement:

  • s/\b(456)\b/ – replaces the standalone number “456” globally in the input line
  • $1 >= 400 ? “999” : $1/ – uses a ternary operator to check whether the captured number $1 is greater than or equal to 400 before performing the replacement with “999” or keeping the original number
  • g – ensures all occurrences of the pattern (not just the first one) in each line are replaced
  • e – allows the evaluation of the replacement expression

In short, this Perl one-liner will correctly replace the number “456” with “999” in the file numbers.txt, but only if the number is greater than or equal to 400.

5.3. Performing Complex Replacements With Callbacks

Perl also allows us to use a callback function for more intricate replacements. This enables us to execute custom code for each match, making it highly flexible.

Suppose we have a file dates.txt containing the text:

Today's date is 2023-07-22.
The event will take place on 2022-12-01.
Another date mentioned: 2021-05-10.

Let’s consider a scenario where we want to replace these dates with the format “yyyy-mm-dd” with the “dd/mm/yyyy” format in our file dates.txt using a Perl one-liner:

$ perl -i -pe 's|(\d{4})-(\d{2})-(\d{2})|$3/$2/$1|' dates.txt

In this command:

  • (\d{4}) – captures four consecutive digits representing the year
  • (\d{2}) – captures the next two consecutive digits representing the month
  • (\d{2}) – captures the next two consecutive digits representing the day
  • (\d{4})-(\d{2})-(\d{2}) – captures the date patterns in the format “yyyy-mm-dd”

When the command runs, it reads dates.txt line by line. For each line, the perl command with the -pe flags applies the regular expression to find occurrences of the date pattern “yyyy-mm-dd.” When it finds such a pattern, it captures the day, month, and year components into three groups.

In the replacement part of the perl command, we rearrange the captured groups using $3/$2/$1, representing “day/month/year”. Finally, we update the original file with the modified content.

6. The ex and ed POSIX Text Editors

In the realm of text editing on POSIX-compliant systems, ex and ed hold a special place as standard text editors. These editors provide powerful find-and-replace functionalities, making them noteworthy alternatives to sed for text manipulation tasks. Let’s see how to utilize ex and ed to perform find-and-replace operations on our Linux systems.

6.1. ex Editor

ex is an extended version of the venerable ed editor, a standard Unix text editor dating back to the early days of computing. It adds numerous enhancements, including interactive and visual modes, making it more user-friendly. Although it may not be as widely used as other text editors today, its find-and-replace capabilities are worth considering.

Let’s look at an example of using ex to perform a find-and-replace operation on a file file.txt:

old_text="our old text"
new_text="our new text"
ex -s file.txt << EOF

In this example:

  • ex -s file.txt << EOF – starts the ex editor in non-interactive mode and opens the file.txt for editing
  • -s – stands for “silent” mode, meaning the editor will not display the usual interactive interface
  • %s/old_text/new_text/g – performs a global find-and-replace by replacing all occurrences of old_text with new_text
  • x – saves the changes and exits the editor

The ex editor will process all these commands between << EOF and EOF, then save the changes to file.txt.

6.2. ed Editor

While ed may appear less user-friendly than modern text editors, its power and efficiency in handling large files and batch editing tasks are also commendable. Like the ex editor, ed is also scriptable, allowing us to automate find-and-replace operations in batch mode.

Let’s see how to perform a find-and-replace operation on the file data.txt with ed:

old_text="our old text"
new_text="our new text"
printf "%s\n" ',s/$old_text/$new_text/g' w q | ed -s data.txt

Here, we use a series of commands within printf to send a script to ed:

  • “%s\n” – format string for printf, where %s is a placeholder for the following text, and \n adds a new line after the text is printed
  • ‘,s/old_text/new_text/g’ – the actual command that performs the global find-and-replace operation
  • w – saves the changes
  • q – quits the ed editor
  • -s – runs the script in script mode without entering interactive mode

Notably, both ex and ed provide robust and efficient means of performing file find-and-replace operations. Their scripting capabilities and availability on almost all POSIX-compliant systems make them dependable alternatives to sed when dealing with text manipulation tasks.

7. Text Replacement With Python

Python, a versatile and popular programming language, offers many libraries and features for text processing and manipulation. With its easy-to-read syntax and powerful capabilities, Python is an excellent alternative to sed for text replacement tasks. Let’s explore how Python can effectively perform find-and-replace operations on our Linux systems.

7.1. Basic Text Replacement With Python

Python provides simple yet effective ways to perform text replacement using the replace method of string objects.

Suppose we have a file data.txt with the following content:

This is the content of data.txt.
It contains some text to replace.
Please replace "text to replace" using a Python script.

For instance, we want to replace “text to replace” with “new text content” in our file data.txt using a Python script:

#!/usr/bin/env python3
with open('data.txt', 'r') as file:
    content = file.read()

content = content.replace('text to replace', 'new text content')

with open('data.txt', 'w') as file:

In this example, we read the content of data.txt into variable content using open and read. Then, we use the replace method to substitute all occurrences of “text to replace” with “new text content” within the content variable. Finally, we write the modified content back to the data.txt file.

To execute this Python script, we create a new Python file and save it in the same directory as data.txt. For this example, we use replace_text.py.

Then, we open a terminal in the directory where both files are present, make the script executable with the chmod command, and then execute the Python script with python3:

$ python3 replace_text.py

After executing the script, we can use the cat command to verify our changes and view the file data.txt:

$ cat data.txt
This is the content of data.txt.
It contains some text to replace.
Please replace "new text content" using a Python script.

As we can see, the script substitutes all occurrences of “text to replace” with “new text content” upon executing the Python script.

7.2. Performing Regular Expression-Based Replacements

Python’s re module provides powerful support for regular expressions, making it well-suited for more complex find-and-replace tasks.

Let’s consider an example where we want to replace dates in the format “yyyy-mm-dd” with “dd/mm/yyyy” in a file dates.txt using a Python script:

#!/usr/bin/env python3
import re

with open('dates.txt', 'r') as file:
    content = file.read()

pattern = r'(\d{4})-(\d{2})-(\d{2})'
replacement = r'\3/\2/\1'
content = re.sub(pattern, replacement, content)

with open('dates.txt', 'w') as file:

In this example, we import the Python re module to work with regular expressions. Similar to our previous interaction in Perl, we define a pattern ‘(\d{4})-(\d{2})-(\d{2})’, capturing the year, month, and day in different groups. Then, the re.sub function performs the substitution using the replacement pattern, reordering the groups as “day/month/year”. The modified content is then written back to the dates.txt file.

In short, Python’s versatility and ease of use make it a compelling choice for text replacement tasks. Whether we’re handling simple find-and-replace operations or more complex regular expression-based substitutions, Python provides a robust set of tools to handle various text manipulation scenarios.

8. Conclusion

Throughout this article, we explored a variety of alternatives to the sed command in Linux. From the classic tr command and shell substitution capabilities to powerful tools like awk, Perl, Python, and the ex and ed editors, each alternative brings its unique strengths to the table.

By leveraging these tools, we can efficiently perform text replacement tasks on our Linux systems at any scale, even when sed is unavailable. So, let’s choose the tool that best fits our specific needs and enjoy seamless text processing on our Linux journey!

Comments are closed on this article!