How to Use the Linux sed Command to Find and Replace

The Massive File Problem

You manage a Linux web server. You just migrated your company’s website from an old domain (old-company.com) to a brand new domain (new-company.com). However, the massive database dump file contains 15,000 hardcoded links to the old domain.

If you try to open a 15,000-line text file using a standard visual editor like nano or vim, the editor will likely freeze. Even if it opens, manually performing a “Find and Replace” (Ctrl+F) operation 15,000 times will take hours and is prone to human error.

To manipulate massive amounts of text in Linux, you never open the file manually. Instead, you use a Stream Editor, universally known as the sed command. It is a legendary Unix utility designed to parse through a text file at blazing speeds, identify a specific pattern of text, and instantly replace it with new text, all without ever physically opening the file on your screen.

The Basic Syntax (Find and Replace)

The sed command uses a very specific syntax structure, separated by forward slashes (/).

The core structure for substituting text looks like this: s/old_text/new_text/

  • s stands for “substitute.”

If you want to replace the word “apple” with the word “orange” inside a file named fruit.txt, you would type this in your terminal:

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

The Global Flag (Fixing the First Match Trap)

If you run the command above, you will discover a major problem. By default, sed only replaces the very first instance of the word “apple” it finds on a single line. If a line says “apple apple apple,” the output will be “orange apple apple.”

To tell sed to replace every single instance of the word on every line, you must append the g (Global) flag at the very end of the slashes.

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

This is the standard command that 99% of developers use daily.

The In-Place Edit Flag (-i)

If you run the command above, sed will instantly spit out the modified text onto your terminal screen. However, if you open fruit.txt, you will find that the original file has not actually changed. sed is protecting you by only outputting a “preview” of the changes.

Once you verify that the preview looks correct, you must use the -i (In-place) flag to force sed to permanently overwrite the actual file on the hard drive.

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

When you hit enter, the terminal will remain completely silent. But if you open the file, every single “apple” has been permanently eradicated.

Solving the Domain Migration Problem

Returning to our original server problem: replacing the old domain with the new domain in the database file.

sed -i 's/old-company.com/new-company.com/g' database.sql

In less than a second, the Linux server will parse all 15,000 lines, rewrite the hardcoded links, and save the file, allowing you to instantly import the corrected database.

Never attempt to manually edit massive server logs or database dumps. By mastering the s/old/new/g syntax of the Linux sed command, you can instantly perform complex Find and Replace operations on thousands of lines of text directly from the command line.

Get the best tech tips delivered straight to your inbox.

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