How to Find and Replace Text in Files Using Sed in Linux

Mastering Text Manipulation from the Command Line

If you regularly work with Linux servers or manage large codebases, you will inevitably need to search for a specific string of text inside a file and replace it with something else. While you could open the file in a text editor like Nano or Vim, doing so manually is incredibly inefficient, especially if you need to replace hundreds of instances across a massive log file or configuration script.

This is where the sed (stream editor) command comes in. It is one of the most powerful and widely used command-line utilities in Linux and Unix-like operating systems. While it has many features, its primary and most common use case is finding and replacing text quickly and accurately.

Here is a complete guide to using the sed command to find and replace text in Linux, from simple single-word replacements to modifying original files safely.

The Basic Syntax of Sed

The standard syntax for replacing text using sed looks like this:

sed 's/search_text/replacement_text/' filename.txt

Let’s break down exactly what this means:

  • sed: Invokes the command.
  • s: Stands for “substitute”. This tells the program you want to perform a search and replace operation.
  • /: The delimiter. It separates the different parts of the command.
  • search_text: The exact word or string you want to find.
  • replacement_text: The new word or string you want to insert.
  • filename.txt: The file you are targeting.

Replacing the First Occurrence on Each Line

If you want to replace only the first time a word appears on a line, you use the basic syntax. For example, if you want to replace the word “apple” with “orange” in a file called fruits.txt, you would run:

sed 's/apple/orange/' fruits.txt

By default, sed will print the results directly to your terminal screen. It will not modify the original fruits.txt file yet. This is a great safety feature that allows you to preview the changes before making them permanent.

Replacing Every Occurrence in the File (Global Replacement)

The command above will only replace the first instance of “apple” on any given line. If the word “apple” appears three times on line 5, only the first one becomes “orange”. To tell sed to replace every single instance throughout the entire line, you must add the g (global) flag at the end of the substitution string.

sed 's/apple/orange/g' fruits.txt

Ignoring Case Sensitivity

By default, sed is case-sensitive. It will treat “Apple”, “apple”, and “APPLE” as entirely different words. If you want to replace the word regardless of how it is capitalized, you can add the i (ignore case) flag. You can also combine it with the global flag.

sed 's/apple/orange/gi' fruits.txt

How to Save the Changes to the File

Once you have run your command and verified in the terminal output that the replacement worked correctly, you will likely want to save those changes. There are two ways to do this.

1. Redirect Output to a New File

The safest method is to use the standard Linux output redirection operator (>) to save the changes into a brand-new file, leaving the original file completely untouched.

sed 's/apple/orange/g' fruits.txt > new_fruits.txt

2. Modify the File in Place

If you are confident in your command and want to overwrite the original file directly, you use the -i (in-place) option before the substitution string.

sed -i 's/apple/orange/g' fruits.txt

Safely Modifying Files with an Automatic Backup

Modifying configuration files in place can be risky. If you make a mistake with a complex regular expression, you could break your system. Fortunately, sed has a built-in safety net. If you provide a file extension directly after the -i option, sed will automatically create a backup of the original file before making any changes.

sed -i.bak 's/apple/orange/g' fruits.txt

This command will perform the find and replace on fruits.txt, but it will also create an untouched backup file named fruits.txt.bak in the same directory.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.