How to Use sed to Find and Replace a String in a File

Try this guide with our instant dedicated server for as low as 40 Euros

Use sed to Find and Replace a String

By providing your email address or using a single sign-on provider to create an account, you agree to our Terms of Service and that you have reviewed our Privacy Policy.

Thank you for contacting us! We will get in touch with you soon.

Developed in 1974 by Lee McMahon of the Bell Labs, Stream Editor (shortened as sed) is a helpful Linux tool used to search and replace (insert/delete) text line-by-line. A common use of this utility is string replacement in files.

Linux users prefer the sed utility to find and replace string/text in a file because of its non-interactive editing format, where there is no need to open or change the original file content. The utility usually outputs the modified text to the standard output (usually the terminal) or redirects it to a new file.

In this tutorial, we’ll explore how to use sed to find and replace a string in a file. We’ll also show several replace string operations and recursive search scenarios.

Table of Contents

  1. Working with sed on macOS
  2. Create the Sample File for This Tutorial
  3. The sed Find and Replace Syntax
  4. How to Use sed to Find and Replace Strings in a File
    1. Basic Replacement
    2. Specific Line Replacement
    3. First Matched String Replacement
    4. Global Replacement
    5. Case-Insensitive Replacement
    6. Strings With Forward Slashes Replacement
    7. Regular Expression Replacement
    8. Recursive Replacement
    9. How to Create a Backup
  5. Conclusion
  6. FAQs

Working with sed on macOS

If you’re on macOS and want to use the GNU version of the sed utility, you can install it via package managers like Homebrew. Run the following command to install the utility:

$ brew install gnu-sed

Next, use gsed with all the commands. For instance:

$ gsed -i ‘s|<pattern>|<substitute>|g’ <target-file>

Create the Sample File for This Tutorial

Let’s first create a sample text file for running the various string replacement scenarios. You can use it to create your favorite text editor. We’ll use the nano editor with the following command:

# nano samplefile.txt

Add the following text to this file:

applebananaapplebanana

apple banana apple banana

Apple Banana Apple Banana

APPLE BANANA APPLE BANANA

/apple/banana /apple/banana

Finally, display the file contents with the cat command:

# cat samplefile.txt

The sed Find and Replace Syntax

A typical sed command to search and replace text has the following basic syntax:

# sed -i s|<pattern>|<substitute>|g’ <target-file>

Let’s break down this syntax to understand it better:

  • -i: The -i flag applies modifications to the file rather than just showing the changes in the standard output.
  • s: Denotes the substitution command.
  • |: Acts as a delimiter. Though / is traditional, using | (or other characters) is recommended if the pattern or replacement has slashes.
  • <pattern>: Represents the text you want to search for within the file. It could be a simple string or a complex expression.
  • <substitute>: This text will replace the matched pattern in the file.
  • g: This stands for “global” and signifies that the substitution should be applied globally in each line. If the g option is omitted, the first occurrence in each line is replaced.
  • <target-file>: This is the file where the search and replace operation will be performed. It’s the file you want to modify using the sed command.

How to Use sed to Find and Replace Strings in a File

Let’s start with the prerequisites for using the sed utility:

  • Basic understanding of the Linux or Unix command-line interface (CLI).
  • Ensure that sed is installed on your system. 

Basic Replacement

# sed ‘s|banana|linux|g’ samplefile.txt

This command replaces all occurrences of banana with linux in samplefile.txt.

Specific Line Replacement

# sed ‘3s|banana|linux|g’ samplefile.txt

Here, 3 denotes the line number. This command replaces banana with linux only on line 3 in the samplefile.txt.

First Matched String Replacement

Execute the following command to replace the very first occurrence of the word banana with linux within each line of the file:

# sed -i s|banana|linux| samplefile.txt

This command will substitute the first occurrence of ‘banana’’ with ‘linux’ within every line, including substrings. It performs an exact match and is case-sensitive, disregarding any variations in capitalization.

The -i option saves the changes directly to the samplefile.txt file. It is a good idea to verify the updated contents with the cat command:

# cat samplefile.txt

string replacement

Do you know that, similar to sed, you can use the awk commands in Linux to edit and replace text patterns. 

Global Replacement

Use the following command to replace all occurrences of the word banana with linux in every line of a file:

# sed -i s|banana|linux|g’ example.txt

Note the g option (abbreviation for global) that  ensures that all instances of banana are replaced within the file. After executing the command, inspect the updated file with the cat command:

# cat samplefile.txt

Global replacement

Case-Insensitive Replacement

To perform a case-insensitive find and replace operation for all occurrences of a word, use the command’s l parameter:

# sed -i s|banana|linux|gI’ samplefile.txt

After executing this command, you can check the updated file contents with the cat command:

# cat samplefile.txt

Case insensitive replacement

Strings With Forward Slashes Replacement

To handle string replacement involving forward slashes, you can opt for one of these two approaches:

  • Escaping forward slashes using the backslashes as delimiter:

# sed -i ‘s|\|apple\|banana|\|redswitches\|home|g’ samplefile.txt

  • Alternatively, opt for a different delimiter to simplify the command:

# sed -i ‘s:|apple|banana:|redswitches|home:g’ samplefile.txt

Here’s the file contents after executing the command: 

# cat samplefile.txt

forward slashes replacement

Regular Expression Replacement

To replace all uppercase letters with the number 3, run the following command:

# sed -i s|[A-Z]|3|g’ samplefile.txt

Run the cat command to see the updated file contents:

# cat samplefile.txt

regular expression replacement

Recursive Replacement

In the context of file operations, recursive means that the action is performed not only on the current directory but also on all the subdirectories and files. 

We recommend using the find command with sed to perform recursive string replacements within files. 

Let’s understand this with the help of an example:

# sed -i -r ‘s/[A-Z]/5/g’ /path/to/directory/*

The * here means the command is applied to all files within this directory and its subdirectories. 

Here’s another command that combines the find command with sed to perform a case-insensitive replacement, changing all lowercase letters to the number 5. You can then inspect the sample file to confirm the command’s process:

recursive replacement

How to Create a Backup

If you don’t want to make changes to the original file as a result of sed operations, you can create a backup of the file before making changes by combining .bak parameter to the -i option:

# sed -i.bak s|apple|APPLE|g’ samplefile.txt

After executing the command that creates backups before making changes, you can confirm the modifications and view both the modified file and its backup using the cat command. Here’s how you can do it:

# cat samplefile.txt.bak

# cat samplefile.txt

creating backup

Conclusion

sed is a versatile text-processing tool for tasks ranging from simple string replacements to complex pattern-based transformations. The examples above introduce how to use Sed to find and replace a string in a file. 

It provides users with a means to manipulate and edit text efficiently, allowing for tasks such as finding and replacing specific strings, deleting or inserting text based on defined patterns, and applying changes across multiple lines.

For those managing large-scale applications, having a reliable hosting provider like RedSwitches ensures your applications run smoothly. It allows efficient handling of data processing tasks covered in this guide. 

Contact us for more information about our robust bare metal server hosting, dedicated server hosting, instant dedicated server hosting, and 10 Gbps dedicated server hosting.

FAQs

Q. What is Sed Command?

Sed, short for Stream Editor, is a powerful command-line tool that edits text by reading it from a file, making changes, and saving the edited text back to a file.

Q. How can I use the Sed command to find and replace text within a file?

To use sed to find and replace text within a file, you can use the following command:

sed ‘s/old_text/new_text/g’ filename

This command replaces all occurrences of ‘old_text’ with ‘new_text’ within the file specified by ‘filename.’

Q. Can sed handle multi-line patterns?

Yes, sed can match patterns that extend across multiple lines due to its advanced regular expression features.

Q. Is there a way to preview changes without modifying the file?

Yes, by default, sed outputs the modified content to the terminal without altering the file. To save the changes, use the -i option.

Q. How does sed compare to awk?

Both are powerful text-processing tools. While sed excels at simple text transformations, awk is more of a programming language designed for complex data extraction and reporting.

Q. What are the syntax and options of the Sed command for finding and replacing text?

The syntax of the sed command for finding and replacing text is as follows:

sed ‘s/old_text/new_text/flags’ filename

The flags that can be used with the sed command are:

s: The substitution flag, which indicates that we want to perform a substitution operation.

g: The global flag, which indicates that we want to replace all occurrences of ‘old_text’ with ‘new_text’ within each line of the file.

Q. Can I use special characters in the text I want to find and replace?

Yes, you can use special characters in the text you want to find and replace. However, you may need to escape certain special characters using backslashes (\) to prevent them from being interpreted as part of the sed command syntax.

Q. Is the sed command available in Linux and macOS?

Yes, the Sed command is available in both Linux and macOS. You can use it in the command-line interface of these operating systems.

Q. Can I use the sed command in a shell script?

Yes, you can use the Sed command in a shell script. You can include the sed command within the script and execute it as part of its logic.

Try this guide with our instant dedicated server for as low as 40 Euros